【使用TIFF库对tif图片的读写】

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);
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xhgen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值