-->

在TIFF创建缩略图一个子IFD(的libtiff)(In a TIFF create a Sub

2019-07-30 23:45发布

我知道thumbnail.c包括创建缩略图,并将其放置在一个子IDF一些代码,但有很多代码回事(生成缩略图,应用对比度曲线等),我有困难只是重现写一个缩略图。 谷歌一直没有任何帮助的。

我的问题是,我已经打开了一个输出文件,并有一个TIFF *后,我有我的缩略图数据都准备好了(以及我的主图像数据),如何添加他们以这样的方式,该缩略图是子主图像IFD的IFD?

Answer 1:

因此,通过在一段时间内的libtiff源代码周围挖掘后,我在tif_dirwrite.c碰到这个偶然发现:

 /*
 * Copyright (c) 1988-1997 Sam Leffler
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 */

...
if (!n)
    return(0);
/*
 * Total hack: if this directory includes a SubIFD
 * tag then force the next <n> directories to be
 * written as ``sub directories'' of this one.  This
 * is used to write things like thumbnails and
 * image masks that one wants to keep out of the
 * normal directory linkage access mechanism.
 */
tif->tif_flags|=TIFF_INSUBIFD;
tif->tif_nsubifd=tif->tif_dir.td_nsubifd;
if (tif->tif_dir.td_nsubifd==1)
    tif->tif_subifdoff=0;
else
    tif->tif_subifdoff=m;
return(1);
...

(我包括版权信息,因为我不知道我是否有从库发布代码时这里)

因此,要回答自己的问题(如何写一个缩略图在主图像IFD的子IFD):

//...
//For the sake of this demo we will assume that I have opened a 
//TIFF (TIFF* created_TIFF) in write mode and have included the correct header
//files

//set all of your TIFF fields for the main image
//...

//Define the number of sub-IFDs you are going to write
//(assuming here that we are only writing one thumbnail for the image):
int number_of_sub_IFDs = 1;
toff_t sub_IFDs_offsets[1] = { 0UL };

//set the TIFFTAG_SUBIFD field:
if(!TIFFSetField(created_TIFF, TIFFTAG_SUBIFD, number_of_sub_IFDs, 
    sub_IFDs_offsets))
{
    //there was an error setting the field
}

//Write your main image raster data to the TIFF (using whatever means you need,
//such as TIFFWriteRawStrip, TIFFWriteEncodedStrip, TIFFWriteEncodedTile, etc.)
//...

//Write your main IFD like so:
TIFFWriteDirectory(created_TIFF);

//Now here is the trick: like the comment in the libtiff source states, the 
//next n directories written will be sub-IFDs of the main IFD (where n is 
//number_of_sub_IFDs specified when you set the TIFFTAG_SUBIFD field)

//Set up your sub-IFD
if(!TIFFSetField(created_TIFF, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE))
{
    //there was an error setting the field
}

//set the rest of the required tags here, as well as any extras you would like
//(remember, these refer to the thumbnail, not the main image)
//...

//Write this sub-IFD:
TIFFWriteDirectory(created_TIFF);

//Assuming you are only writing one sub-IFD and are done with the file, you 
//can close it now. If you specified more than one sub-IFD, you need repeat 
//the above code (starting where we set TIFFTAG_SUBFILETYPE) for each of your
//sub-IFDs
TIFFClose(created_TIFF);

我希望这可以帮助别人,他们没有像我一样弄清楚如何做到这一点花费同样多的努力。 这真的是一个耻辱,被填实libtiff如此糟糕记录,特别是考虑到它是如何得到广泛应用。



文章来源: In a TIFF create a Sub IFD with thumbnail (libtiff)