2012-08-14 47 views
7

我知道thumbnail.c包含一些創建縮略圖並將其放置在子IDF中的代碼,但該代碼中還有很多事情要做(生成縮略圖,應用對比曲線等),並且我很難再現只寫一個縮略圖。谷歌也沒有任何幫助。在TIFF中創建一個帶縮略圖的子IFD(libtiff)

我的問題是,在我打開輸出文件並且有TIFF *之後,我已經準備好了我的縮略圖數據(以及我的主要圖像數據),我如何以這種方式添加它們縮略圖位於主圖像IFD的子IFD中?

回答

5

所以通過在一段時間內的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是如此糟糕的記錄,特別是考慮它的使用範圍。