Blame html/addingtags.html

Packit 85355f
<HTML>
Packit 85355f
<HEAD>
Packit 85355f
<TITLE>
Packit 85355f
Modifying The TIFF Library
Packit 85355f
</TITLE>
Packit 85355f
</HEAD>
Packit 85355f
<BODY BGCOLOR=white> 
Packit 85355f
<FONT FACE="Arial, Helvetica, Sans">
Packit 85355f
Packit 85355f

Packit 85355f
Defining New TIFF Tags
Packit 85355f
Packit 85355f
Packit 85355f
Libtiff has built-in knowledge of all the standard TIFF tags, as
Packit 85355f
well as extentions.  The following describes how to add knowledge of
Packit 85355f
new tags as builtins to libtiff, or how to application specific tags can
Packit 85355f
be used by applications without modifying libtiff. 
Packit 85355f

Packit 85355f
Packit 85355f

TIFFFieldInfo

Packit 85355f
Packit 85355f
How libtiff manages specific tags is primarily controlled by the 
Packit 85355f
definition for that tag value stored internally as a TIFFFieldInfo structure. 
Packit 85355f
This structure looks like this:
Packit 85355f

Packit 85355f
Packit 85355f
Packit 85355f
typedef	struct {
Packit 85355f
  ttag_t    field_tag;          /* field's tag */
Packit 85355f
  short	    field_readcount;    /* read count/TIFF_VARIABLE/TIFF_SPP */
Packit 85355f
  short	    field_writecount;   /* write count/TIFF_VARIABLE */
Packit 85355f
  TIFFDataType field_type;      /* type of associated data */
Packit 85355f
  unsigned short field_bit;     /* bit in fieldsset bit vector */
Packit 85355f
  unsigned char field_oktochange;/* if true, can change while writing */
Packit 85355f
  unsigned char field_passcount;/* if true, pass dir count on set */
Packit 85355f
  char	*field_name;		/* ASCII name */
Packit 85355f
} TIFFFieldInfo;
Packit 85355f
Packit 85355f
Packit 85355f
    Packit 85355f
  • field_tag: the tag number. For instance 277 for the
  • Packit 85355f
    SamplesPerPixel tag.  Builtin tags will generally have a #define in
    Packit 85355f
    tiff.h for each known tag. 

    Packit 85355f
    Packit 85355f
  • field_readcount: The number of values which should be read.
  • Packit 85355f
    The special value TIFF_VARIABLE (-1) indicates that a variable number of
    Packit 85355f
    values may be read.  The special value TIFFTAG_SPP (-2) indicates that there
    Packit 85355f
    should be one value for each sample as defined by TIFFTAG_SAMPLESPERPIXEL.  
    Packit 85355f
    The special value TIFF_VARIABLE2 (-3) is presumably similar to TIFF_VARIABLE
    Packit 85355f
    though I am not sure what the distinction in behaviour is.  This field
    Packit 85355f
    is TIFF_VARIABLE for variable length ascii fields.

    Packit 85355f
    Packit 85355f
  • field_writecount: The number of values which should be written.
  • Packit 85355f
    Generally the same as field_readcount.  A few built-in exceptions exist, but
    Packit 85355f
    I haven't analysed why they differ. 

    Packit 85355f
    Packit 85355f
  • field_type: Type of the field. One of TIFF_BYTE, TIFF_ASCII,
  • Packit 85355f
    TIFF_SHORT, TIFF_LONG, TIFF_RATIONAL, TIFF_SBYTE, TIFF_UNDEFINED, 
    Packit 85355f
    TIFF_SSHORT, TIFF_SLONG, TIFF_SRATIONAL, TIFF_FLOAT, TIFF_DOUBLE or
    Packit 85355f
    TIFF_IFD.  Note that some fields can support more than one type (for 
    Packit 85355f
    instance short and long).  These fields should have multiple TIFFFieldInfos. 
    Packit 85355f

    Packit 85355f
    Packit 85355f
  • field_bit: Built-in tags stored in special fields in the
  • Packit 85355f
    TIFF structure have assigned field numbers to distinguish them (ie. 
    Packit 85355f
    FIELD_SAMPLESPERPIXEL).  New tags should generally just use 
    Packit 85355f
    FIELD_CUSTOM indicating they are stored in the generic tag list.

    Packit 85355f
    Packit 85355f
  • field_oktochange: TRUE if it is OK to change this tag value
  • Packit 85355f
    while an image is being written.  FALSE for stuff that must be set once
    Packit 85355f
    and then left unchanged (like ImageWidth, or PhotometricInterpretation for
    Packit 85355f
    instance).

    Packit 85355f
    Packit 85355f
  • field_passcount: If TRUE, then the count value must be passed
  • Packit 85355f
    in TIFFSetField(), and TIFFGetField(), otherwise the count is not required.
    Packit 85355f
    This should generally be TRUE for non-ascii variable count tags unless
    Packit 85355f
    the count is implicit (such as with the colormap).

    Packit 85355f
    Packit 85355f
  • field_name: A name for the tag. Normally mixed case (studly caps)
  • Packit 85355f
    like "StripByteCounts" and relatively short. 

    Packit 85355f
    Packit 85355f
    Packit 85355f
    Packit 85355f
    A TIFFFieldInfo definition exists for each built-in tag in the tif_dirinfo.c
    Packit 85355f
    file.  Some tags which support multiple data types have more than one
    Packit 85355f
    definition, one per data type supported. 

    Packit 85355f
    Packit 85355f
    Various functions exist for getting the internal TIFFFieldInfo definitions,
    Packit 85355f
    including _TIFFFindFieldInfo(), and _TIFFFindFieldInfoByName().  See
    Packit 85355f
    tif_dirinfo.c for details.  There must be some mechanism to get the whole
    Packit 85355f
    list, though I don't see it off hand.

    Packit 85355f
    Packit 85355f

    Default Tag Auto-registration

    Packit 85355f
    Packit 85355f
    In libtiff 3.6.0 a new mechanism was introduced allowing libtiff to 
    Packit 85355f
    read unrecognised tags automatically.  When an unknown tags is encountered, 
    Packit 85355f
    it is automatically internally defined with a default name and a type 
    Packit 85355f
    derived from the tag value in the file.  Applications only need to predefine
    Packit 85355f
    application specific tags if they need to be able to set them in a file, or
    Packit 85355f
    if particular calling conventions are desired for TIFFSetField() and 
    Packit 85355f
    TIFFGetField().

    Packit 85355f
    Packit 85355f
    When tags are autodefined like this the field_readcount and
    Packit 85355f
    field_writecount values are always TIFF_VARIABLE.  The 
    Packit 85355f
    field_passcount is always TRUE, and the field_bit is 
    Packit 85355f
    FIELD_CUSTOM.  The field name will be "Tag %d" where the %d is the tag 
    Packit 85355f
    number.

    Packit 85355f
    Packit 85355f

    Defining Application Tags

    Packit 85355f
    Packit 85355f
    For various reasons, it is common for applications to want to define
    Packit 85355f
    their own tags to store information outside the core TIFF specification. 
    Packit 85355f
    This is done by calling TIFFMergeFieldInfo() with one or more TIFFFieldInfos. 
    Packit 85355f

    Packit 85355f
    Packit 85355f
    The libgeotiff library provides geospatial information extentions within
    Packit 85355f
    a TIFF file.  First, a set of TIFFFieldInfo's is prepared with information
    Packit 85355f
    on the new tags:

    Packit 85355f
    Packit 85355f
    Packit 85355f
    static const TIFFFieldInfo xtiffFieldInfo[] = {
    Packit 85355f
      
    Packit 85355f
      /* XXX Insert Your tags here */
    Packit 85355f
        { TIFFTAG_GEOPIXELSCALE,	-1,-1, TIFF_DOUBLE,	FIELD_CUSTOM,
    Packit 85355f
          TRUE,	TRUE,	"GeoPixelScale" },
    Packit 85355f
        { TIFFTAG_GEOTRANSMATRIX,	-1,-1, TIFF_DOUBLE,	FIELD_CUSTOM,
    Packit 85355f
          TRUE,	TRUE,	"GeoTransformationMatrix" },
    Packit 85355f
        { TIFFTAG_GEOTIEPOINTS,	-1,-1, TIFF_DOUBLE,	FIELD_CUSTOM,
    Packit 85355f
          TRUE,	TRUE,	"GeoTiePoints" },
    Packit 85355f
        { TIFFTAG_GEOKEYDIRECTORY, -1,-1, TIFF_SHORT,	FIELD_CUSTOM,
    Packit 85355f
          TRUE,	TRUE,	"GeoKeyDirectory" },
    Packit 85355f
        { TIFFTAG_GEODOUBLEPARAMS,	-1,-1, TIFF_DOUBLE,	FIELD_CUSTOM,
    Packit 85355f
          TRUE,	TRUE,	"GeoDoubleParams" },
    Packit 85355f
        { TIFFTAG_GEOASCIIPARAMS,	-1,-1, TIFF_ASCII,	FIELD_CUSTOM,
    Packit 85355f
          TRUE,	FALSE,	"GeoASCIIParams" }
    Packit 85355f
    };
    Packit 85355f
    Packit 85355f
    Packit 85355f
    In order to define the tags, we call TIFFMergeFieldInfo() on the
    Packit 85355f
    desired TIFF handle with the list of TIFFFieldInfos.

    Packit 85355f
    Packit 85355f
    Packit 85355f
    #define	N(a)	(sizeof (a) / sizeof (a[0]))
    Packit 85355f
    Packit 85355f
        /* Install the extended Tag field info */
    Packit 85355f
        TIFFMergeFieldInfo(tif, xtiffFieldInfo, N(xtiffFieldInfo));
    Packit 85355f
    Packit 85355f
    Packit 85355f
    The tags need to be defined for each TIFF file opened - and when reading
    Packit 85355f
    they should be defined before the tags of the file are read, yet a valid
    Packit 85355f
    TIFF * is needed to merge the tags against.   In order to get them 
    Packit 85355f
    registered at the appropriate part of the setup process, it is necessary
    Packit 85355f
    to register our merge function as an extender callback with libtiff. 
    Packit 85355f
    This is done with TIFFSetTagExtender().  We also keep track of the 
    Packit 85355f
    previous tag extender (if any) so that we can call it from our extender
    Packit 85355f
    allowing a chain of customizations to take effect. 

    Packit 85355f
    Packit 85355f
    Packit 85355f
    static TIFFExtendProc _ParentExtender = NULL;
    Packit 85355f
    Packit 85355f
    static
    Packit 85355f
    void _XTIFFInitialize(void)
    Packit 85355f
    {
    Packit 85355f
        static int first_time=1;
    Packit 85355f
    	
    Packit 85355f
        if (! first_time) return; /* Been there. Done that. */
    Packit 85355f
        first_time = 0;
    Packit 85355f
    	
    Packit 85355f
        /* Grab the inherited method and install */
    Packit 85355f
        _ParentExtender = TIFFSetTagExtender(_XTIFFDefaultDirectory);
    Packit 85355f
    }
    Packit 85355f
    Packit 85355f
    Packit 85355f
    The extender callback is looks like this.  It merges in our new fields
    Packit 85355f
    and then calls the next extender if there is one in effect.

    Packit 85355f
    Packit 85355f
    Packit 85355f
    static void
    Packit 85355f
    _XTIFFDefaultDirectory(TIFF *tif)
    Packit 85355f
    {
    Packit 85355f
        /* Install the extended Tag field info */
    Packit 85355f
        TIFFMergeFieldInfo(tif, xtiffFieldInfo, N(xtiffFieldInfo));
    Packit 85355f
    Packit 85355f
        /* Since an XTIFF client module may have overridden
    Packit 85355f
         * the default directory method, we call it now to
    Packit 85355f
         * allow it to set up the rest of its own methods.
    Packit 85355f
         */
    Packit 85355f
    Packit 85355f
        if (_ParentExtender) 
    Packit 85355f
            (*_ParentExtender)(tif);
    Packit 85355f
    }
    Packit 85355f
    Packit 85355f
    Packit 85355f
    The above approach ensures that our new definitions are used when reading
    Packit 85355f
    or writing any TIFF file.  However, since on reading we already have 
    Packit 85355f
    default definitions for tags, it is usually not critical to pre-define them.
    Packit 85355f
    If tag definitions are only required for writing custom tags, you can just
    Packit 85355f
    call TIFFMergeFieldInfo() before setting new tags.  The whole extender
    Packit 85355f
    architecture can then be avoided.

    Packit 85355f
    Packit 85355f

    Adding New Builtin Tags

    Packit 85355f
    Packit 85355f
    A similar approach is taken to the above.  However, the TIFFFieldInfo 
    Packit 85355f
    should be added to the tiffFieldInfo[] list in tif_dirinfo.c.  Ensure that
    Packit 85355f
    new tags are added in sorted order by the tag number.

    Packit 85355f
    Packit 85355f
    Normally new built-in tags should be defined with FIELD_CUSTOM; however, if
    Packit 85355f
    it is desirable for the tag value to have it's own field in the TIFFDirectory
    Packit 85355f
    structure, then you will need to #define a new FIELD_ value for it, and
    Packit 85355f
    add appropriate handling as follows:
    Packit 85355f
    Packit 85355f
    Packit 85355f
      Packit 85355f
    1. Define the tag in tiff.h.
    2. Packit 85355f
    3. Add a field to the directory structure in tif_dir.h
    4. Packit 85355f
         and define a <TT>FIELD_*</TT> bit (also update the definition of
      Packit 85355f
         <TT>FIELD_CODEC</TT> to reflect your addition).
      Packit 85355f
    5. Add an entry in the <TT>TIFFFieldInfo</TT> array defined at the top of
    6. Packit 85355f
         tif_dirinfo.c. 
      Packit 85355f
         Note that you must keep this array sorted by tag
      Packit 85355f
         number and that the widest variant entry for a tag should come
      Packit 85355f
         first (e.g. <TT>LONG</TT> before <TT>SHORT</TT>).
      Packit 85355f
    7. Add entries in <TT>_TIFFVSetField()</TT> and <TT>_TIFFVGetField()</TT>
    8. Packit 85355f
         for the new tag.
      Packit 85355f
    9. (optional) If the value associated with the tag is not a scalar value
    10. Packit 85355f
         (e.g. the array for <TT>TransferFunction</TT>) and requires
      Packit 85355f
         special processing,
      Packit 85355f
         then add the appropriate code to <TT>TIFFReadDirectory()</TT> and
      Packit 85355f
         <TT>TIFFWriteDirectory()</TT>.  You're best off finding a similar tag and
      Packit 85355f
         cribbing code.
      Packit 85355f
    11. Add support to <TT>TIFFPrintDirectory()</TT> in tif_print.c
    12. Packit 85355f
          to print the tag's value.
      Packit 85355f
      Packit 85355f
      Packit 85355f

      Packit 85355f
      If you want to maintain portability, beware of making assumptions
      Packit 85355f
      about data types.  Use the typedefs (<TT>uint16</TT>, etc. when dealing with
      Packit 85355f
      data on disk and <TT>t*_t</TT> when stuff is in memory) and be careful about
      Packit 85355f
      passing items through printf or similar vararg interfaces.
      Packit 85355f
      Packit 85355f

      Adding New Codec-private Tags

      Packit 85355f
      Packit 85355f
      To add tags that are meaningful only when a particular compression
      Packit 85355f
      algorithm is used follow these steps:
      Packit 85355f
      Packit 85355f
        Packit 85355f
      1. Define the tag in tiff.h.
      2. Packit 85355f
      3. Allocate storage for the tag values in the private state block of
      4. Packit 85355f
           the codec.
        Packit 85355f
      5. Insure the state block is created when the codec is initialized.
      6. Packit 85355f
      7. At <TT>TIFFInitfoo</TT> time override the method pointers in the
      8. Packit 85355f
            TIFF structure
        Packit 85355f
           for getting, setting and printing tag values.  For example,
        Packit 85355f
        Packit 85355f
            sp->vgetparent = tif->tif_vgetfield;
        Packit 85355f
            tif->tif_vgetfield = fooVGetField;	/* hook for codec tags */
        Packit 85355f
            sp->vsetparent = tif->tif_vsetfield;
        Packit 85355f
            tif->tif_vsetfield = fooVSetField;	/* hook for codec tags */
        Packit 85355f
            tif->tif_printdir = fooPrintDir;	/* hook for codec tags */
        Packit 85355f
        Packit 85355f
           (Actually you may decide not to override the
        Packit 85355f
           <TT>tif_printdir</TT> method, but rather just specify it).
        Packit 85355f
      9. Create a private <TT>TIFFFieldInfo</TT> array for your tags and
      10. Packit 85355f
            merge them into the core tags at initialization time using
        Packit 85355f
            <TT>_TIFFMergeFieldInfo</TT>; e.g.
        Packit 85355f
        Packit 85355f
            _TIFFMergeFieldInfo(tif, fooFieldInfo, N(fooFieldInfo));
        Packit 85355f
        Packit 85355f
           (where <TT>N</TT> is a macro used liberaly throughout the distributed code).
        Packit 85355f
      11. Fill in the get and set routines. Be sure to call the parent method
      12. Packit 85355f
           for tags that you are not handled directly.  Also be sure to set the
        Packit 85355f
           <TT>FIELD_*</TT> bits for tags that are to be written to the file.  Note that
        Packit 85355f
           you can create ``pseudo-tags'' by defining tags that are processed
        Packit 85355f
           exclusively in the get/set routines and never written to file (see
        Packit 85355f
           the handling of <TT>TIFFTAG_FAXMODE</TT> in tif_fax3.c
        Packit 85355f
           for an example of this).
        Packit 85355f
      13. Fill in the print routine, if appropriate.
      14. Packit 85355f
        Packit 85355f
        Packit 85355f
        Note that space has been allocated in the <TT>FIELD_*</TT> bit space for
        Packit 85355f
        codec-private tags.  Define your bits as <TT>FIELD_CODEC+<offset></TT> to
        Packit 85355f
        keep them away from the core tags.  If you need more tags than there
        Packit 85355f
        is room for, just increase <TT>FIELD_SETLONGS</TT> at the top of
        Packit 85355f
        tiffiop.h.
        Packit 85355f
        Packit 85355f

        Packit 85355f
        Packit 85355f
        Last updated: $Date: 2016-09-25 20:05:44 $
        Packit 85355f
        Packit 85355f
        </BODY>
        Packit 85355f
        Packit 85355f
        </HTML>