简体   繁体   English

在TIFF中创建带缩略图的子IFD(libtiff)

[英]In a TIFF create a Sub IFD with thumbnail (libtiff)

I know thumbnail.c includes some code that creates a thumbnail and places it in a sub IDF, but there is a lot going on in that code (generating the thumbnail, applying a contrast curve, etc.) and I am having difficulty reproducing just writing a thumbnail. 我知道thumbnail.c包含一些创建缩略图并将其放在子IDF中的代码,但是代码中有很多(生成缩略图,应用对比度曲线等)并且我很难再现写一个缩略图。 Google has not been any help either. 谷歌也没有任何帮助。

My question is, after I have opened an output file and have a TIFF*, I have my thumbnail data all ready to go (as well as my main image data), how do I add them in such a way that the thumbnail is in a sub IFD of the main image IFD? 我的问题是,在我打开一个输出文件并且有一个TIFF *之后,我的缩略图数据都已准备就绪(以及我的主图像数据),如何以缩略图所在的方式添加它们主图像IFD的子IFD?

So after digging around through the libtiff source code for a while, I stumbled across this in tif_dirwrite.c: 因此,在深入了解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);
...

(I included the copyright info because I wasn't sure if I had to when posting code from the library here) (我包含版权信息,因为我不确定在这里从库中发布代码时是否需要)

So, to answer my own question (how to write a thumbnail in a sub-IFD of the main image IFD): 那么,回答我自己的问题(如何在主图像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);

I hope that this helps somebody and that they don't have to expend as much effort as I did to figure out how to do this. 我希望这对某些人有所帮助,并且他们不必花费尽可能多的努力来弄清楚如何做到这一点。 It really is a shame that libtiff is so poorly documented, especially considering how widely it is used. 令人遗憾的是,诽谤的记录很差,特别是考虑到它的使用范围。

I think @KSletmoe has pointed out a key point that the SubIFD tag(330) should be added in IFD0 before you write any subIFD, but there is still a problem. 我认为@KSletmoe已经指出了一个关键点,即在编写任何subIFD之前应该在IFD0中添加SubIFD标记(330),但仍然存在问题。

The definition of SubIFD tag is "Offset to child IFDs". SubIFD标记的定义是“偏移到子IFD”。 So, if you don't set the offset correctly, tiff parser can not parse the whole tiff correctly. 因此,如果您没有正确设置偏移量,则tiff解析器无法正确解析整个tiff。

There are two ways to handle this situation. 有两种方法可以处理这种情况。

First, pre-calculate the size of each IFD/SubIFD, then fill-in to SubIFD tag when you set, instead of setting 0x0. 首先,预先计算每个IFD / SubIFD的大小,然后在设置时填写SubIFD标签,而不是设置0x0。

Or you can write every IFD as normal then go back to IFD0 and add SubIFD tag with final offset that calculated by libtiff. 或者您可以正常编写每个IFD,然后返回IFD0并添加由libtiff计算的最终偏移量的SubIFD标记。 Like following: 如下:

//Write down every thing you need in a tiff
// ... IFD0
TIFFWriteDirectory(tif);
// ... IFD1
TIFFWriteDirectory(tif);
// ... IFD2
TIFFWriteDirectory(tif);

//set current Dir as IFD0 in the end
TIFFSetDirectory(tif, 0);  

//get next dir offset
sub_offset[1] = TIFFGetNextDirOff(tif, 2);
sub_offset[0] = TIFFGetNextDirOff(tif, 1);
//only for clean next dir offset in IFD0
no_use_offset = TIFFGetNextDirOff(tif, 0);                                                    

TIFFSetField(tif, TIFFTAG_SUBIFD, 2, sub_offset);    

You may need to add flowing function in tif_dir.c 您可能需要在tif_dir.c中添加流动函数

uint64 TIFFGetNextDirOff(TIFF* tif, uint16 dirn)                                                 
{                                                                                                
    uint64 nextdir;                                                                              
    uint16 n;                                                                                    
    if (!(tif->tif_flags&TIFF_BIGTIFF))                                                          
        nextdir = tif->tif_header.classic.tiff_diroff;                                           
    else                                                                                         
        nextdir = tif->tif_header.big.tiff_diroff;                                               
    for (n = dirn; n > 0 && nextdir != 0; n--)                                                   
        if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))                                          
            return (0);

    /*!!Watchout!! Here will reset the next dir offset to 0*/
    tif->tif_nextdiroff = 0;                                                                                                                                     
    return nextdir;                                                                              
}

Thanks for @KSletmoe. 谢谢@KSletmoe。 Hope this can help someone, and looking forward some much elegant way to do so. 希望这可以帮助某人,并期待一些非常优雅的方式来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM