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_dirwrite.c < prev    next >
Text File  |  1996-11-18  |  22KB  |  792 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_dirwrite.c,v 1.31 93/08/26 14:24:07 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 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.  * Directory Write Support Routines.
  33.  */
  34. #include "tiffiop.h"
  35.  
  36. #if HAVE_IEEEFP
  37. #define    TIFFCvtNativeToIEEEFloat(tif, n, fp)
  38. #endif
  39.  
  40. static    int TIFFWriteNormalTag(TIFF*, TIFFDirEntry*, const TIFFFieldInfo*);
  41. static    void TIFFSetupShortLong(TIFF*, ttag_t, TIFFDirEntry*, uint32);
  42. static    int TIFFSetupShortPair(TIFF*, ttag_t, TIFFDirEntry*);
  43. static    int TIFFWriteRational(TIFF*,
  44.         TIFFDataType, ttag_t, TIFFDirEntry*, float);
  45. static    int TIFFWritePerSampleShorts(TIFF*, ttag_t, TIFFDirEntry*);
  46. static    int TIFFWriteShortTable(TIFF*, ttag_t, TIFFDirEntry*, uint32, uint16**);
  47. static    int TIFFWriteShortArray(TIFF*,
  48.         TIFFDataType, ttag_t, TIFFDirEntry*, uint32, uint16*);
  49. static    int TIFFWriteLongArray(TIFF *,
  50.         TIFFDataType, ttag_t, TIFFDirEntry*, uint32, uint32*);
  51. static    int TIFFWriteRationalArray(TIFF *,
  52.         TIFFDataType, ttag_t, TIFFDirEntry*, uint32, float*);
  53. static    int TIFFWriteFloatArray(TIFF *,
  54.         TIFFDataType, ttag_t, TIFFDirEntry*, uint32, float*);
  55. static    int TIFFWriteString(TIFF*, ttag_t, TIFFDirEntry*, char*);
  56. #ifdef JPEG_SUPPORT
  57. static    int TIFFWriteJPEGQTables(TIFF*, TIFFDirEntry*);
  58. static    int TIFFWriteJPEGCTables(TIFF*, ttag_t, TIFFDirEntry*, u_char**);
  59. #endif
  60. #ifdef COLORIMETRY_SUPPORT
  61. static    int TIFFWriteTransferFunction(TIFF*, TIFFDirEntry*);
  62. #endif
  63. static    int TIFFWriteData(TIFF*, TIFFDirEntry*, char*);
  64. static    int TIFFLinkDirectory(TIFF*);
  65.  
  66. #define    WriteRationalPair(type, tag1, v1, tag2, v2) {        \
  67.     if (!TIFFWriteRational(tif, type, tag1, dir, v1))    \
  68.         goto bad;                    \
  69.     if (!TIFFWriteRational(tif, type, tag2, dir+1, v2))    \
  70.         goto bad;                    \
  71.     dir++;                            \
  72. }
  73.  
  74. /*
  75.  * Write the contents of the current directory
  76.  * to the specified file.  This routine doesn't
  77.  * handle overwriting a directory with auxiliary
  78.  * storage that's been changed.
  79.  */
  80. int
  81. TIFFWriteDirectory(TIFF* tif)
  82. {
  83.     short dircount, tag;
  84.     int nfields, dirsize;
  85.     char *data;
  86.     const TIFFFieldInfo *fip;
  87.     TIFFDirEntry *dir;
  88.     TIFFDirectory *td;
  89.     u_long b, fields[FIELD_SETLONGS];
  90.  
  91.     if (tif->tif_mode == O_RDONLY)
  92.         return (1);
  93.     /*
  94.      * Clear write state so that subsequent images with
  95.      * different characteristics get the right buffers
  96.      * setup for them.
  97.      */
  98.     if (tif->tif_flags & TIFF_POSTENCODE) {
  99.         tif->tif_flags &= ~TIFF_POSTENCODE;
  100.         if (tif->tif_postencode && !(*tif->tif_postencode)(tif)) {
  101.             TIFFError(tif->tif_name,
  102.                 "Error post-encoding before directory write");
  103.             return (0);
  104.         }
  105.     }
  106.     if (tif->tif_close)
  107.         (*tif->tif_close)(tif);
  108.     if (tif->tif_cleanup)
  109.         (*tif->tif_cleanup)(tif);
  110.     /*
  111.      * Flush any data that might have been written
  112.      * by the compression close+cleanup routines.
  113.      */
  114.     if (tif->tif_rawcc > 0 && !TIFFFlushData1(tif)) {
  115.         TIFFError(tif->tif_name,
  116.             "Error flushing data before directory write");
  117.         return (0);
  118.     }
  119.     if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
  120.         _TIFFfree(tif->tif_rawdata);
  121.         tif->tif_rawdata = NULL;
  122.         tif->tif_rawcc = 0;
  123.     }
  124.     tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
  125.  
  126.     td = &tif->tif_dir;
  127.     /*
  128.      * Size the directory so that we can calculate
  129.      * offsets for the data items that aren't kept
  130.      * in-place in each field.
  131.      */
  132.     nfields = 0;
  133.     for (b = 0; b <= FIELD_LAST; b++)
  134.         if (TIFFFieldSet(tif, b))
  135.             nfields += (b < FIELD_SUBFILETYPE ? 2 : 1);
  136.     dirsize = nfields * sizeof (TIFFDirEntry);
  137.     data = _TIFFmalloc(dirsize);
  138.     if (data == NULL) {
  139.         TIFFError(tif->tif_name,
  140.             "Cannot write directory, out of space");
  141.         return (0);
  142.     }
  143.     /*
  144.      * Directory hasn't been placed yet, put
  145.      * it at the end of the file and link it
  146.      * into the existing directory structure.
  147.      */
  148.     if (tif->tif_diroff == 0 && !TIFFLinkDirectory(tif))
  149.         goto bad;
  150.     tif->tif_dataoff =
  151.         tif->tif_diroff + sizeof (short) + dirsize + sizeof (long);
  152.     if (tif->tif_dataoff & 1)
  153.         tif->tif_dataoff++;
  154.     (void) TIFFSeekFile(tif, tif->tif_dataoff, L_SET);
  155.     tif->tif_curdir++;
  156.     dir = (TIFFDirEntry *)data;
  157.     /*
  158.      * Setup external form of directory
  159.      * entries and write data items.
  160.      */
  161.     memcpy(fields, td->td_fieldsset, sizeof (fields));
  162.     /*
  163.      * Write out ExtraSamples tag only if
  164.      * extra samples are present in the data.
  165.      */
  166.     if (FieldSet(fields, FIELD_EXTRASAMPLES) && td->td_extrasamples) {
  167.         ResetFieldBit(fields, FIELD_EXTRASAMPLES);
  168.         nfields--;
  169.         dirsize -= sizeof (TIFFDirEntry);
  170.     }                                /*XXX*/
  171.     for (fip = tiffFieldInfo; fip->field_tag; fip++) {
  172.         if (fip->field_bit == FIELD_IGNORE ||
  173.             !FieldSet(fields, fip->field_bit))
  174.             continue;
  175.         switch (fip->field_bit) {
  176.         case FIELD_STRIPOFFSETS:
  177.             /*
  178.              * We use one field bit for both strip and tile
  179.              * offsets, and so must be careful in selecting
  180.              * the appropriate field descriptor (so that tags
  181.              * are written in sorted order).
  182.              */
  183.             tag = isTiled(tif) ?
  184.                 TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS;
  185.             if (tag != fip->field_tag)
  186.                 continue;
  187.             if (!TIFFWriteLongArray(tif, TIFF_LONG, tag, dir,
  188.                 (uint32) td->td_nstrips, td->td_stripoffset))
  189.                 goto bad;
  190.             break;
  191.         case FIELD_STRIPBYTECOUNTS:
  192.             /*
  193.              * We use one field bit for both strip and tile
  194.              * byte counts, and so must be careful in selecting
  195.              * the appropriate field descriptor (so that tags
  196.              * are written in sorted order).
  197.              */
  198.             tag = isTiled(tif) ?
  199.                 TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS;
  200.             if (tag != fip->field_tag)
  201.                 continue;
  202.             if (!TIFFWriteLongArray(tif, TIFF_LONG, tag, dir,
  203.                 (uint32) td->td_nstrips, td->td_stripbytecount))
  204.                 goto bad;
  205.             break;
  206.         case FIELD_ROWSPERSTRIP:
  207.             TIFFSetupShortLong(tif, TIFFTAG_ROWSPERSTRIP,
  208.                 dir, td->td_rowsperstrip);
  209.             break;
  210.         case FIELD_COLORMAP:
  211.             if (!TIFFWriteShortTable(tif, TIFFTAG_COLORMAP, dir,
  212.                 3, td->td_colormap))
  213.                 goto bad;
  214.             break;
  215.         case FIELD_IMAGEDIMENSIONS:
  216.             TIFFSetupShortLong(tif, TIFFTAG_IMAGEWIDTH,
  217.                 dir++, td->td_imagewidth);
  218.             TIFFSetupShortLong(tif, TIFFTAG_IMAGELENGTH,
  219.                 dir, td->td_imagelength);
  220.             break;
  221.         case FIELD_TILEDIMENSIONS:
  222.             TIFFSetupShortLong(tif, TIFFTAG_TILEWIDTH,
  223.                 dir++, td->td_tilewidth);
  224.             TIFFSetupShortLong(tif, TIFFTAG_TILELENGTH,
  225.                 dir, td->td_tilelength);
  226.             break;
  227.         case FIELD_POSITION:
  228.             WriteRationalPair(TIFF_RATIONAL,
  229.                 TIFFTAG_XPOSITION, td->td_xposition,
  230.                 TIFFTAG_YPOSITION, td->td_yposition);
  231.             break;
  232.         case FIELD_RESOLUTION:
  233.             WriteRationalPair(TIFF_RATIONAL,
  234.                 TIFFTAG_XRESOLUTION, td->td_xresolution,
  235.                 TIFFTAG_YRESOLUTION, td->td_yresolution);
  236.             break;
  237.         case FIELD_BITSPERSAMPLE:
  238.         case FIELD_MINSAMPLEVALUE:
  239.         case FIELD_MAXSAMPLEVALUE:
  240.         case FIELD_SAMPLEFORMAT:
  241.             if (!TIFFWritePerSampleShorts(tif, fip->field_tag, dir))
  242.                 goto bad;
  243.             break;
  244.         case FIELD_PAGENUMBER:
  245.         case FIELD_HALFTONEHINTS:
  246. #ifdef YCBCR_SUPPORT
  247.         case FIELD_YCBCRSUBSAMPLING:
  248. #endif
  249. #ifdef CMYK_SUPPORT
  250.         case FIELD_DOTRANGE:
  251. #endif
  252.             if (!TIFFSetupShortPair(tif, fip->field_tag, dir))
  253.                 goto bad;
  254.             break;
  255. #ifdef JPEG_SUPPORT
  256.         case FIELD_JPEGQTABLES:
  257.             if (!TIFFWriteJPEGQTables(tif, dir))
  258.                 goto bad;
  259.             break;
  260.         case FIELD_JPEGDCTABLES:
  261.             if (!TIFFWriteJPEGCTables(tif,
  262.                 TIFFTAG_JPEGDCTABLES, dir, td->td_dctab))
  263.                 goto bad;
  264.             break;
  265.         case FIELD_JPEGACTABLES:
  266.             if (!TIFFWriteJPEGCTables(tif,
  267.                 TIFFTAG_JPEGACTABLES, dir, td->td_actab))
  268.                 goto bad;
  269.             break;
  270. #endif
  271. #ifdef COLORIMETRY_SUPPORT
  272.         case FIELD_TRANSFERFUNCTION:
  273.             if (!TIFFWriteTransferFunction(tif, dir))
  274.                 goto bad;
  275.             break;
  276. #endif
  277.         default:
  278.             if (!TIFFWriteNormalTag(tif, dir, fip))
  279.                 goto bad;
  280.             break;
  281.         }
  282.         dir++;
  283.         ResetFieldBit(fields, fip->field_bit);
  284.     }
  285.     /*
  286.      * Write directory.
  287.      */
  288.     (void) TIFFSeekFile(tif, tif->tif_diroff, L_SET);
  289.     dircount = nfields;
  290.     if (!WriteOK(tif, &dircount, sizeof (short))) {
  291.         TIFFError(tif->tif_name, "Error writing directory count");
  292.         goto bad;
  293.     }
  294.     if (!WriteOK(tif, data, dirsize)) {
  295.         TIFFError(tif->tif_name, "Error writing directory contents");
  296.         goto bad;
  297.     }
  298.     if (!WriteOK(tif, &tif->tif_nextdiroff, sizeof (long))) {
  299.         TIFFError(tif->tif_name, "Error writing directory link");
  300.         goto bad;
  301.     }
  302.     TIFFFreeDirectory(tif);
  303.     _TIFFfree(data);
  304.     tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  305.  
  306.     /*
  307.      * Reset directory-related state for subsequent
  308.      * directories.
  309.      */
  310.     TIFFDefaultDirectory(tif);
  311.     tif->tif_diroff = 0;
  312.     tif->tif_curoff = 0;
  313.     tif->tif_row = -1;
  314.     tif->tif_curstrip = -1;
  315.     return (1);
  316. bad:
  317.     _TIFFfree(data);
  318.     return (0);
  319. }
  320. #undef WriteRationalPair
  321.  
  322. /*
  323.  * Process tags that are not special cased.
  324.  */
  325. static int
  326. TIFFWriteNormalTag(TIFF* tif, TIFFDirEntry* dir, const TIFFFieldInfo* fip)
  327. {
  328.     TIFFDirectory* td = &tif->tif_dir;
  329.     u_short wc = (u_short) fip->field_writecount;
  330.  
  331.     dir->tdir_tag = fip->field_tag;
  332.     dir->tdir_type = (u_short)fip->field_type;
  333.     dir->tdir_count = wc;
  334. #define    WRITE(x,y)    x(tif, fip->field_type, fip->field_tag, dir, wc, y)
  335.     switch (fip->field_type) {
  336.     case TIFF_SHORT:
  337.     case TIFF_SSHORT:
  338.         if (wc > 1) {
  339.             uint16 *wp;
  340.             if (wc == (u_short) TIFF_VARIABLE) {
  341.                 _TIFFgetfield(td, fip->field_tag, &wc, &wp);
  342.                 dir->tdir_count = wc;
  343.             } else
  344.                 _TIFFgetfield(td, fip->field_tag, &wp);
  345.             if (!WRITE(TIFFWriteShortArray, wp))
  346.                 return (0);
  347.         } else {
  348.             uint16 sv;
  349.             _TIFFgetfield(td, fip->field_tag, &sv);
  350.             dir->tdir_offset =
  351.                 TIFFInsertData(tif, dir->tdir_type, sv);
  352.         }
  353.         break;
  354.     case TIFF_LONG:
  355.     case TIFF_SLONG:
  356.         if (wc > 1) {
  357.             uint32 *lp;
  358.             if (wc == (u_short) TIFF_VARIABLE) {
  359.                 _TIFFgetfield(td, fip->field_tag, &wc, &lp);
  360.                 dir->tdir_count = wc;
  361.             } else
  362.                 _TIFFgetfield(td, fip->field_tag, &lp);
  363.             if (!WRITE(TIFFWriteLongArray, lp))
  364.                 return (0);
  365.         } else {
  366.             /* XXX handle LONG->SHORT conversion */
  367.             _TIFFgetfield(td, fip->field_tag, &dir->tdir_offset);
  368.         }
  369.         break;
  370.     case TIFF_RATIONAL:
  371.     case TIFF_SRATIONAL:
  372.         if (wc > 1) {
  373.             float *fp;
  374.             if (wc == (u_short) TIFF_VARIABLE) {
  375.                 _TIFFgetfield(td, fip->field_tag, &wc, &fp);
  376.                 dir->tdir_count = wc;
  377.             } else
  378.                 _TIFFgetfield(td, fip->field_tag, &fp);
  379.             if (!WRITE(TIFFWriteRationalArray, fp))
  380.                 return (0);
  381.         } else {
  382.             float fv;
  383.             _TIFFgetfield(td, fip->field_tag, &fv);
  384.             if (!TIFFWriteRational(tif, fip->field_type, fip->field_tag, dir, fv))
  385.                 return (0);
  386.         }
  387.         break;
  388.     case TIFF_FLOAT:
  389.         if (wc > 1) {
  390.             float *fp;
  391.             if (wc == (u_short) TIFF_VARIABLE) {
  392.                 _TIFFgetfield(td, fip->field_tag, &wc, &fp);
  393.                 dir->tdir_count = wc;
  394.             } else
  395.                 _TIFFgetfield(td, fip->field_tag, &fp);
  396.             if (!WRITE(TIFFWriteFloatArray, fp))
  397.                 return (0);
  398.         } else {
  399.             float fv;
  400.             _TIFFgetfield(td, fip->field_tag, &fv);
  401.             TIFFCvtNativeToIEEEFloat(tif, 1, &fv);
  402.             /* XXX assumes sizeof (long) == sizeof (float) */
  403.             dir->tdir_offset = *(uint32*) &fv;    /* XXX */
  404.         }
  405.         break;
  406.     case TIFF_ASCII: {
  407.         char *cp;
  408.         _TIFFgetfield(td, fip->field_tag, &cp);
  409.         if (!TIFFWriteString(tif, fip->field_tag, dir, cp))
  410.             return (0);
  411.         break;
  412.     }
  413.     }
  414.     return (1);
  415. }
  416. #undef WRITE
  417.  
  418. /*
  419.  * Setup a directory entry with either a SHORT
  420.  * or LONG type according to the value.
  421.  */
  422. static void
  423. TIFFSetupShortLong(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, uint32 v)
  424. {
  425.     dir->tdir_tag = tag;
  426.     dir->tdir_count = 1;
  427.     if (v > 0xffffL) {
  428.         dir->tdir_type = (short)TIFF_LONG;
  429.         dir->tdir_offset = v;
  430.     } else {
  431.         dir->tdir_type = (short)TIFF_SHORT;
  432.         dir->tdir_offset = TIFFInsertData(tif, (int)TIFF_SHORT, v);
  433.     }
  434. }
  435. #undef MakeShortDirent
  436.  
  437. /*
  438.  * Setup a RATIONAL directory entry and
  439.  * write the associated indirect value.
  440.  */
  441. static int
  442. TIFFWriteRational(TIFF* tif,
  443.     TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, float v)
  444. {
  445.     return (TIFFWriteRationalArray(tif, type, tag, dir, 1, &v));
  446. }
  447.  
  448. #define    NITEMS(x)    (sizeof (x) / sizeof (x[0]))
  449. /*
  450.  * Setup a directory entry that references a
  451.  * samples/pixel array of SHORT values and
  452.  * (potentially) write the associated indirect
  453.  * values.
  454.  */
  455. static int
  456. TIFFWritePerSampleShorts(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
  457. {
  458.     uint16 buf[10], v;
  459.     uint16* w = buf;
  460.     int i, status, samples = tif->tif_dir.td_samplesperpixel;
  461.  
  462.     if (samples > NITEMS(buf))
  463.         w = (uint16*)_TIFFmalloc(samples * sizeof (uint16));
  464.     _TIFFgetfield(&tif->tif_dir, tag, &v);
  465.     for (i = 0; i < samples; i++)
  466.         w[i] = v;
  467.     status = TIFFWriteShortArray(tif, TIFF_SHORT, tag, dir, samples, w);
  468.     if (w != buf)
  469.         _TIFFfree((char*)w);
  470.     return (status);
  471. }
  472. #undef NITEMS
  473.  
  474. /*
  475.  * Setup a pair of shorts that are returned by
  476.  * value, rather than as a reference to an array.
  477.  */
  478. static int
  479. TIFFSetupShortPair(TIFF* tif, ttag_t tag, TIFFDirEntry* dir)
  480. {
  481.     uint16 v[2];
  482.  
  483.     _TIFFgetfield(&tif->tif_dir, tag, &v[0], &v[1]);
  484.     return (TIFFWriteShortArray(tif, TIFF_SHORT, tag, dir, 2, v));
  485. }
  486.  
  487. /*
  488.  * Setup a directory entry for an NxM table of shorts,
  489.  * where M is known to be 2**bitspersample, and write
  490.  * the associated indirect data.
  491.  */
  492. static int
  493. TIFFWriteShortTable(TIFF* tif,
  494.     ttag_t tag, TIFFDirEntry* dir, uint32 n, uint16** table)
  495. {
  496.     uint32 i, off;
  497.  
  498.     dir->tdir_tag = tag;
  499.     dir->tdir_type = (short)TIFF_SHORT;
  500.     /* XXX -- yech, fool TIFFWriteData */
  501.     dir->tdir_count = 1L<<tif->tif_dir.td_bitspersample;
  502.     off = tif->tif_dataoff;
  503.     for (i = 0; i < n; i++)
  504.         if (!TIFFWriteData(tif, dir, (char *)table[i]))
  505.             return (0);
  506.     dir->tdir_count *= n;
  507.     dir->tdir_offset = off;
  508.     return (1);
  509. }
  510.  
  511. /*
  512.  * Setup a directory entry of an ASCII string
  513.  * and write any associated indirect value.
  514.  */
  515. static int
  516. TIFFWriteString(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, char* cp)
  517. {
  518.     dir->tdir_tag = tag;
  519.     dir->tdir_type = (short)TIFF_ASCII;
  520.     dir->tdir_count = strlen(cp) + 1;    /* includes \0 byte */
  521.     if (dir->tdir_count > 4) {
  522.         if (!TIFFWriteData(tif, dir, cp))
  523.             return (0);
  524.     } else
  525.         memcpy(&dir->tdir_offset, cp, dir->tdir_count);
  526.     return (1);
  527. }
  528.  
  529. /*
  530.  * Setup a directory entry of an array of SHORT
  531.  * or SSHORT and write the associated indirect values.
  532.  */
  533. static int
  534. TIFFWriteShortArray(TIFF* tif,
  535.     TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, uint16* v)
  536. {
  537.     dir->tdir_tag = tag;
  538.     dir->tdir_type = (short)type;
  539.     dir->tdir_count = n;
  540.     if (n <= 2) {
  541.         if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  542.             dir->tdir_offset = (long)v[0] << 16;
  543.             if (n == 2)
  544.                 dir->tdir_offset |= v[1] & 0xffff;
  545.         } else {
  546.             dir->tdir_offset = v[0] & 0xffff;
  547.             if (n == 2)
  548.                 dir->tdir_offset |= (long)v[1] << 16;
  549.         }
  550.         return (1);
  551.     } else
  552.         return (TIFFWriteData(tif, dir, (char *)v));
  553. }
  554.  
  555. /*
  556.  * Setup a directory entry of an array of LONG
  557.  * or SLONG and write the associated indirect values.
  558.  */
  559. static int
  560. TIFFWriteLongArray(TIFF* tif,
  561.     TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, uint32* v)
  562. {
  563.     dir->tdir_tag = tag;
  564.     dir->tdir_type = (short)type;
  565.     dir->tdir_count = n;
  566.     if (n == 1) {
  567.         dir->tdir_offset = v[0];
  568.         return (1);
  569.     } else
  570.         return (TIFFWriteData(tif, dir, (char *)v));
  571. }
  572.  
  573. /*
  574.  * Setup a directory entry of an array of RATIONAL
  575.  * or SRATIONAL and write the associated indirect values.
  576.  */
  577. static int
  578. TIFFWriteRationalArray(TIFF* tif,
  579.     TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, float* v)
  580. {
  581.     uint32 i;
  582.     uint32* t;
  583.     int status;
  584.  
  585.     dir->tdir_tag = tag;
  586.     dir->tdir_type = (short)type;
  587.     dir->tdir_count = n;
  588.     t = (uint32*)_TIFFmalloc(2*n * sizeof (uint32));
  589.     for (i = 0; i < n; i++) {
  590.         float fv = v[i];
  591.         int sign = 1;
  592.         uint32 den;
  593.  
  594.         if (fv < 0) {
  595.             if (type == TIFF_RATIONAL) {
  596.                 TIFFWarning(tif->tif_name,
  597.     "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL",
  598.                     TIFFFieldWithTag(tag)->field_name, v);
  599.                 fv = 0;
  600.             } else
  601.                 fv = -fv, sign = -1;
  602.         }
  603.         den = 1L;
  604.         if (fv > 0) {
  605.             while (fv < 1L<<(31-3) && den < 1L<<(31-3))
  606.                 fv *= 1<<3, den *= 1L<<3;
  607.         }
  608.         t[2*i+0] = sign * (fv + 0.5);
  609.         t[2*i+1] = den;
  610.     }
  611.     status = TIFFWriteData(tif, dir, (char *)t);
  612.     _TIFFfree((char *)t);
  613.     return (status);
  614. }
  615.  
  616. static int
  617. TIFFWriteFloatArray(TIFF* tif,
  618.     TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, float* v)
  619. {
  620.     dir->tdir_tag = tag;
  621.     dir->tdir_type = (short)type;
  622.     dir->tdir_count = n;
  623.     TIFFCvtNativeToIEEEFloat(tif, n, v);
  624.     if (n == 1) {
  625.         dir->tdir_offset = *(uint32*)&v[0];
  626.         return (1);
  627.     } else
  628.         return (TIFFWriteData(tif, dir, (char *)v));
  629. }
  630.  
  631. #ifdef JPEG_SUPPORT
  632. #define    NITEMS(x)    (sizeof (x) / sizeof (x[0]))
  633. /*
  634.  * Setup a directory entry for JPEG Quantization
  635.  * tables and write the associated indirect values.
  636.  */
  637. static int
  638. TIFFWriteJPEGQTables(TIFF* tif, TIFFDirEntry* dir)
  639. {
  640.     int i, status = 0, samples = tif->tif_dir.td_samplesperpixel;
  641.     uint32 buf[10], *off = buf;
  642.     TIFFDirEntry tdir;
  643.  
  644.     tdir.tdir_tag = TIFFTAG_JPEGQTABLES;    /* for diagnostics */
  645.     tdir.tdir_type = (short)TIFF_BYTE;
  646.     tdir.tdir_count = 64;
  647.     if (samples > NITEMS(buf))
  648.         off = (uint32*)_TIFFmalloc(samples * sizeof (uint32));
  649.     for (i = 0; i < samples; i++) {
  650.         if (!TIFFWriteData(tif, &tdir, (char *)tif->tif_dir.td_qtab[i]))
  651.             goto bad;
  652.         off[i] = tdir.tdir_offset;
  653.     }
  654.     status = TIFFWriteLongArray(tif, TIFF_LONG,
  655.         TIFFTAG_JPEGQTABLES, dir, samples, off);
  656. bad:
  657.     if (off != buf)
  658.         _TIFFfree((char*)off);
  659.     return (status);
  660. }
  661.  
  662. /*
  663.  * Setup a directory entry for JPEG Coefficient
  664.  * tables and write the associated indirect values.
  665.  */
  666. static int
  667. TIFFWriteJPEGCTables(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, u_char** tab)
  668. {
  669.     int status = 0, samples = tif->tif_dir.td_samplesperpixel;
  670.     uint32 buf[10], *off = buf;
  671.     TIFFDirEntry tdir;
  672.     int i, j, ncodes;
  673.  
  674.     tdir.tdir_tag = tag;        /* for diagnostics */
  675.     tdir.tdir_type = (short)TIFF_BYTE;
  676.     if (samples > NITEMS(buf))
  677.         off = (uint32*)_TIFFmalloc(samples * sizeof (uint32));
  678.     for (i = 0; i < samples; i++) {
  679.         for (ncodes = 0, j = 0; j < 16; j++)
  680.             ncodes += tab[i][j];
  681.         tdir.tdir_count = 16+ncodes;
  682.         if (!TIFFWriteData(tif, &tdir, (char *)tab[i]))
  683.             goto bad;
  684.         off[i] = tdir.tdir_offset;
  685.     }
  686.     status = TIFFWriteLongArray(tif, TIFF_LONG, tag, dir, samples, off);
  687. bad:
  688.     if (off != buf)
  689.         _TIFFfree((char*)off);
  690.     return (status);
  691. }
  692. #undef NITEMS
  693. #endif
  694.  
  695. #ifdef COLORIMETRY_SUPPORT
  696. static int
  697. TIFFWriteTransferFunction(TIFF* tif, TIFFDirEntry* dir)
  698. {
  699.     TIFFDirectory* td = &tif->tif_dir;
  700.     uint32 n = (1L<<td->td_bitspersample) * sizeof (uint16);
  701.     uint16** tf = td->td_transferfunction;
  702.     int ncols;
  703.  
  704.     /*
  705.      * Check if the table can be written as a single column,
  706.      * or if it must be written as 3 columns.  Note that we
  707.      * write a 3-column tag if there are 2 samples/pixel and
  708.      * a single column of data won't suffice--hmm.
  709.      */
  710.     switch (td->td_samplesperpixel - td->td_extrasamples) {
  711.     default:    if (memcmp(tf[0], tf[2], n)) { ncols = 3; break; }
  712.     case 2:        if (memcmp(tf[0], tf[1], n)) { ncols = 3; break; }
  713.     case 1: case 0:    ncols = 1;
  714.     }
  715.     return (TIFFWriteShortTable(tif,
  716.         TIFFTAG_TRANSFERFUNCTION, dir, ncols, tf));
  717. }
  718. #endif
  719.  
  720. /*
  721.  * Write a contiguous directory item.
  722.  */
  723. static int
  724. TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp)
  725. {
  726.     tsize_t cc;
  727.  
  728.     dir->tdir_offset = tif->tif_dataoff;
  729.     cc = dir->tdir_count * tiffDataWidth[dir->tdir_type];
  730.     if (SeekOK(tif, dir->tdir_offset) &&
  731.         WriteOK(tif, cp, cc)) {
  732.         tif->tif_dataoff += (cc + 1) & ~1;
  733.         return (1);
  734.     }
  735.     TIFFError(tif->tif_name, "Error writing data for field \"%s\"",
  736.         TIFFFieldWithTag(dir->tdir_tag)->field_name);
  737.     return (0);
  738. }
  739.  
  740. /*
  741.  * Link the current directory into the
  742.  * directory chain for the file.
  743.  */
  744. static int
  745. TIFFLinkDirectory(TIFF* tif)
  746. {
  747.     static const char module[] = "TIFFLinkDirectory";
  748.     uint16 dircount;
  749.     uint32 nextdir;
  750.  
  751.     tif->tif_diroff = (TIFFSeekFile(tif, 0L, L_XTND)+1) &~ 1L;
  752.     if (tif->tif_header.tiff_diroff == 0) {
  753.         /*
  754.          * First directory, overwrite header.
  755.          */
  756.         tif->tif_header.tiff_diroff = tif->tif_diroff;
  757.         (void) TIFFSeekFile(tif, 0L, L_SET);
  758.         if (!WriteOK(tif, &tif->tif_header,
  759.             sizeof (tif->tif_header))) {
  760.             TIFFError(tif->tif_name, "Error writing TIFF header");
  761.             return (0);
  762.         }
  763.         return (1);
  764.     }
  765.     /*
  766.      * Not the first directory, search to the last and append.
  767.      */
  768.     nextdir = tif->tif_header.tiff_diroff;
  769.     do {
  770.         if (!SeekOK(tif, nextdir) ||
  771.             !ReadOK(tif, &dircount, sizeof (uint16))) {
  772.             TIFFError(module, "Error fetching directory count");
  773.             return (0);
  774.         }
  775.         if (tif->tif_flags & TIFF_SWAB)
  776.             TIFFSwabShort(&dircount);
  777.         TIFFSeekFile(tif, dircount * sizeof (TIFFDirEntry), L_INCR);
  778.         if (!ReadOK(tif, &nextdir, sizeof (uint32))) {
  779.             TIFFError(module, "Error fetching directory link");
  780.             return (0);
  781.         }
  782.         if (tif->tif_flags & TIFF_SWAB)
  783.             TIFFSwabLong(&nextdir);
  784.     } while (nextdir != 0);
  785.     (void) TIFFSeekFile(tif, -sizeof (nextdir), L_INCR);
  786.     if (!WriteOK(tif, &tif->tif_diroff, sizeof (tif->tif_diroff))) {
  787.         TIFFError(module, "Error writing directory link");
  788.         return (0);
  789.     }
  790.     return (1);
  791. }
  792.