该代码主要使用libtiff库,将T.6 格式文件转换成 T.4格式。
int tiff2tiff(char *pSrcFile, char *pDstFile)
{
TIFF* intiff;
TIFF* outiff;
uint32 width, height;
uint16 bitspersample = 1;
uint16 samplesperpixel = 1;
uint16 compession = 1;
float xresolution, yresolution; //分辨率
int nTotalFrame;
uint16 pageNum;
tsize_t stripSize;
uint32 imageOffset, result;
int stripMax, stripCount;
char *buffer;
uint32 bufferSize;
if ((intiff = TIFFOpen(pSrcFile, "r")) == NULL)
{
return 0;
}
if ((outiff = TIFFOpen(pDstFile, "w")) == NULL)
{
return 0;
}
//获取源文件的配置信息
TIFFGetField(intiff, TIFFTAG_IMAGEWIDTH, &width); // 图片得到宽度
TIFFGetField(intiff, TIFFTAG_IMAGELENGTH, &height); // 图片得到高度
TIFFGetField(intiff, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); // bits per channel (sample) 每个通道(样本)(1bit或者8bit)
TIFFGetField(intiff, TIFFTAG_BITSPERSAMPLE, &bitspersample); // samples per pixel 每像素采样(1通道或者3通道)
TIFFGetField(intiff, TIFFTAG_XRESOLUTION, &xresolution); // x分辨率
TIFFGetField(intiff, TIFFTAG_YRESOLUTION, &yresolution); // y分辨率
nTotalFrame = TIFFNumberOfDirectories(intiff); // 获取文件总页数
// Read in the possibly multiple strips
stripSize = TIFFStripSize(intiff);
stripMax = TIFFNumberOfStrips(intiff);
bufferSize = TIFFNumberOfStrips(intiff) * stripSize;
if ((buffer = (char*)malloc(bufferSize)) == NULL)
{
//CONV_Assert(__LINE__, "Could not allocate enough memory for the uncompressed image\n");
return 0;
}
//将fax4图片信息分页写入到fax3中
for (pageNum = 0; pageNum < nTotalFrame; pageNum++)
{
TIFFSetDirectory(intiff, pageNum);
TIFFSetDirectory(outiff, pageNum);
imageOffset = 0;
for (stripCount = 0; stripCount < stripMax; stripCount++)
{
if ((result = TIFFReadEncodedStrip(intiff, stripCount, buffer + imageOffset, stripSize)) == -1) //获取此页面数据
{
free(buffer);
//CONV_Assert(__LINE__, "Read error on input strip number %d\n", stripCount);
return 0;
}
imageOffset += result;
}
TIFFSetField(outiff, TIFFTAG_PAGENUMBER, pageNum); //页面数
TIFFSetField(outiff, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(outiff, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(outiff, TIFFTAG_BITSPERSAMPLE, bitspersample);
TIFFSetField(outiff, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
TIFFSetField(outiff, TIFFTAG_ROWSPERSTRIP, height);
TIFFSetField(outiff, TIFFTAG_COMPRESSION, COMPRESSION_CCITT_T4); //压缩格式
TIFFSetField(outiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
TIFFSetField(outiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
TIFFSetField(outiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(outiff, TIFFTAG_XRESOLUTION, xresolution); //像素分辨率-X
TIFFSetField(outiff, TIFFTAG_YRESOLUTION, yresolution); //像素分辨率-Y
TIFFSetField(outiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
TIFFWriteEncodedStrip(outiff, 0, buffer, bufferSize); //写文件
TIFFWriteDirectory(outiff);
}
//关闭文件
TIFFClose(outiff);
TIFFClose(intiff);
free(buffer);
return 1;
}