c++ - 在TIFF中,创建带有缩略图的Sub IFD(libtiff)

标签 c++ thumbnails tiff libtiff

我知道thumbnail.c包含一些创建缩略图并将其放置在子IDF中的代码,但是该代码中有很多事情(生成缩略图,应用对比度曲线等),我很难复制编写缩略图。 Google也没有任何帮助。

我的问题是,在打开输出文件并具有TIFF *之后,我已经准备好要处理的缩略图数据(以及我的主图像数据),如何以缩略图形式添加它们?主图像IFD的子IFD?

最佳答案

因此,在研究了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);

我希望这可以对某人有所帮助,并且他们不必像我一样花很多精力去解决如何做到这一点。事实证明如此之薄,这实在可惜,尤其是考虑到它的使用范围。

关于c++ - 在TIFF中,创建带有缩略图的Sub IFD(libtiff),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11959617/

相关文章:

html - 关于在 HTML 中渲染 .tiff 图像的问题

c++ - 检测TIFF图像中每个像素的位数

c++ - 链接(LNK1000)时出现非常奇怪的错误?

php - 在PHP中没有ffmpeg的情况下从视频创建缩略图

android - Android Picasso-从网络下载视频缩略图

video - 与 Shell 脚本一起使用时在哪里添加 FilterChain

image - 有没有办法在不读取整个文件的情况下推断文件是什么图像格式?

c++ - CMake无法找到SDL2_ttf,我正在尝试以与SDL2相同的方式链接它,并且两者都已正确安装在Ubuntu 18.04上

c++ - int_least64_t vs int_fast64_t vs int64_t

c++ - 如何使用CImg显示少量图像(每个图像在单独的窗口中)?