读取8位图片,将unsigned char * buffer改成unsigned short *buffer可以读取16位图片
bool TDatTransform::LoadSliceImage(const char path[], unsigned char * 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);
}
TIFFClose(tif);
}
return true;
}
写8位,16 tif图片
oid TDatTransform::Write8BitImage(const char *path, unsigned char* buffer, uint32 width, uint32 height)//写16位图片需改成uint16 * buffer
{
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位图片//改成16可写16图片
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];//16位图片是这里需要改成uint16 * curRow = new uint16[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);
}
三维tif写函数
bool TDatTransform::Write3DBuffer16bit(const char filename[], uint16 * buffer)
{
TIFFSetWarningHandler(0);
TIFF* out = TIFFOpen(filename, "w");
if (out)
{
size_t nWidth = BLOCK_SIZE;
size_t nHeight = BLOCK_SIZE;
size_t nTotalFrame = DEPTH_SIZE;
//size_t wh = nWidth * nHeight;
uint16 *temp = new uint16[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_RESOLUTIONUNIT, 2);
/*TIFFSetField(out, TIFFTAG_YRESOLUTION, 196.0f);
TIFFSetField(out, TIFFTAG_XRESOLUTION, 204.0f);*/
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
// 设置一个样本所占内存的大小.
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 16);
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);
TIFFWriteEncodedStrip(out, 0, &buffer[N_size], nWidth * nHeight * 2);
//TIFFWriteRawStrip (out, 0, temp, nWidth * nHeight);
++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;
}
}