当前位置: 首页 > 工具软件 > libtiff > 使用案例 >

libtiff 使用教程

钱钊
2023-12-01

vs 2017 编译好的工程

vs 2017 cmake 编译方式

​
/*-----------------------------------tif库打开图像测试代码-start--------------------------------*/
#include "../libtiff/tiffio.h" 
int readTiff(char*path)
{
	TIFF *tifFile = TIFFOpen(path, "r");  //提前在工程目录下放入 p.tif 的文件,“r”表示读写权限为“读”

	int width, height;
	TIFFGetField(tifFile, TIFFTAG_IMAGEWIDTH, &width);
	TIFFGetField(tifFile, TIFFTAG_IMAGELENGTH, &height);

	int pixelCount = width * height;
	unsigned char *R = new unsigned char[pixelCount];
	unsigned char *G = new unsigned char[pixelCount];
	unsigned char *B = new unsigned char[pixelCount];
	unsigned char *A = new unsigned char[pixelCount];
	unsigned int *image = new unsigned int[pixelCount];
	TIFFReadRGBAImage(tifFile, width, height, image, 1);

	memset(R, 0, pixelCount);
	memset(G, 0, pixelCount);
	memset(B, 0, pixelCount);
	memset(A, 0, pixelCount);

	unsigned int *rowPointerToSrc = image + width * (height - 1);
	unsigned char *rowPointerToR = R;
	unsigned char *rowPointerToG = G;
	unsigned char *rowPointerToB = B;
	unsigned char *rowPointerToA = A;
	for (int y = 0; y < height; y++)
	{
		unsigned int *colPointerToSrc = rowPointerToSrc;
		unsigned char *colPointerToR = rowPointerToR;
		unsigned char *colPointerToG = rowPointerToG;
		unsigned char *colPointerToB = rowPointerToB;
		unsigned char *colPointerToA = rowPointerToA;

		cout << "第 " << y + 1 << " 行:" << endl;
		for (int x = 0; x < width; x++)
		{
			colPointerToR[0] = (unsigned char)TIFFGetR(colPointerToSrc[0]);
			colPointerToG[0] = (unsigned char)TIFFGetG(colPointerToSrc[0]);
			colPointerToB[0] = (unsigned char)TIFFGetB(colPointerToSrc[0]);
			colPointerToA[0] = (unsigned char)TIFFGetA(colPointerToSrc[0]);

			cout << "\tR:" << (int)colPointerToR[0] << "\tG:" << (int)colPointerToG[0] << "\tB:" << (int)colPointerToB[0] << "\tA:" << (int)colPointerToA[0] << endl;
			colPointerToSrc++;
			colPointerToR++;
			colPointerToG++;
			colPointerToB++;
			colPointerToA++;
		}
		rowPointerToSrc -= width;
		rowPointerToR += width;
		rowPointerToG += width;
		rowPointerToB += width;
		rowPointerToA += width;
	}
	delete image;
	delete R;
	delete G;
	delete B;
	delete A;

	return 0;
}
/*-----------------------------------tif库打开图像测试代码-end--------------------------------*/

​
void writeTif(const char *path, ushort* buffer, uint width, uint height)
{
	TIFF *tif = TIFFOpen(path, "w");
	TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
	TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
	TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); //每个像素点应有几个通道
	TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16); //每个通道的位宽,这里为16位图片//改成8可写8位图片
	TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_LEFTTOP);
	TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
	TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); //设定图片显示时,灰度值越小的像素越接近黑色
	TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, width));

	TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
	//整体输出
	TIFFWriteEncodedStrip(tif, 0, (uchar*)buffer, width*height * 2);
	//按行输出
	//ushort* curRow = new ushort[width];//16位图片是这里需要改成uint16 * curRow = new uint16[width];
	//for (uint32 row = 0; row < height; row++)
	//{
	//	memcpy(curRow, (buffer + row * width), width*2);    // check the index here, and figure out why not using h*linebytes
	//	if (TIFFWriteScanline(tif, curRow, row, 0) < 0)
	//		break;
	//}
	//delete[]curRow;

	TIFFClose(tif);
}

 类似资料: