TIFF库下载地址:www.libtif.org
tif图片的读写
#include<tiffio.h>
//图片读取,16为图片和8位图片一样,将buffer改成8位的即可
bool TDatTransform::LoadSliceImage16bit(const char path[], uint16 * buffer, int width, int height)
{
TIFFSetWarningHandler(0);
TIFF *tif = TIFFOpen(path, "r");
if (tif == 0) {
printf(" open TIFF file %s failed\n", path);
}
else
{
uint32 row;
for (row = 0; row <(uint32)height; row++)
{
//需要注意,这里内存偏移必须写为(row-ys)*width
TIFFReadScanline(tif, (buffer + row*width), row);//指针位置,读取长度,三维的话也是这样,加个z方向偏移量即可
}
TIFFClose(tif);
}
return true;
}
//写tif图,16位图将其中8位部分数据改成16位即可,三维图片
bool TDatTransform::Write3DBuffer(const char filename[], unsigned char * buffer,size_t nWidth,size_t nHeight,size_t nTotalFrame)
{
TIFFSetWarningHandler(0);
TIFF* out = TIFFOpen(filename, "w");
if (out)
{
//size_t wh = nWidth * nHeight;
uint8 *temp = new uint8[nWidth * nHeight];
int N_size = 0;
size_t nCur = 0;
//UChar den = (sizeof(T) == 1) ? 1 : 4;
do{
TIFFSetField(out, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
TIFFSetField(out, TIFFTAG_PAGENUMBER, nTotalFrame);
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32)nWidth);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32)nHeight);
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
// 设置一个样本所占内存的大小.
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, nHeight);
unsigned char* curRow = new unsigned char[nWidth];
for (uint32 row = 0; row < nHeight; row++)
{
memcpy(curRow, (&buffer[N_size] + row*nWidth), nWidth); // check the index here, and figure out why not using h*linebytes
if (TIFFWriteScanline(out, curRow, row, 0) < 0)
break;
}
delete[]curRow;
++nCur;
N_size = N_size + nWidth*nHeight;
} while (TIFFWriteDirectory(out) && nCur < nTotalFrame);
delete[] temp;
TIFFClose(out);
return true;
}
else
{
printf("cant create tiff!\n");
return false;
}
}
//二维图片的写操作
void TDatTransform::Write8BitImage(const char *path, unsigned char* buffer, uint32 width, uint32 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, 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_ROWSPERSTRIP, 1);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
unsigned char* curRow = new unsigned char[width];
for (uint32 row = 0; row < height; row++)
{
memcpy(curRow, (buffer + row*width), width); // check the index here, and figure out why not using h*linebytes
if (TIFFWriteScanline(tif, curRow, row, 0) < 0)
break;
}
delete[]curRow;
TIFFClose(tif);
}