使用libtiff库生成tif文件,格式转换。

一、说明

该代码主要使用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;
}

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用libtiff保存tif格式的图像,可以使用以下代码: ```c++ #include "tiffio.h" #include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; void save_tiff(const string& filename, const Mat& image) { TIFF* tif = TIFFOpen(filename.c_str(), "w"); if (tif) { int width = image.cols; int height = image.rows; int channels = image.channels(); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, channels); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); for (int y = 0; y < height; y++) { TIFFWriteScanline(tif, (unsigned char*)image.ptr(y), y); } TIFFClose(tif); } } int main() { Mat image = imread("test.jpg"); if (image.empty()) { cout << "Failed to read image!" << endl; return -1; } // 旋转图像 Mat rotated = rotate(image, 45); // 保存为tif格式 save_tiff("test.tif", rotated); // 读取并显示tif格式图像 Mat image_tif = imread("test.tif", IMREAD_ANYDEPTH | IMREAD_ANYCOLOR); if (image_tif.empty()) { cout << "Failed to read tiff image!" << endl; return -1; } imshow("TIF image", image_tif); waitKey(0); return 0; } ``` 这个例子中,我们首先使用opencv读取一张jpg格式的图像,然后对它进行旋转,然后使用save_tiff函数保存为tif格式的图像。在保存过程中,我们使用libtiff的函数来设置图像的各种属性,然后使用TIFFWriteScanline函数逐行写入图像数据。最后,我们再使用opencv读取保存的tif格式图像并显示。 在读取tif格式图像时,需要使用IMREAD_ANYDEPTH和IMREAD_ANYCOLOR参数来读取图像的深度和颜色信息。如果不指定这些参数,则可能会导致图像显示不正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值