使用libtiff读tiff图RGBA四通道数据

    资源链接:http://download.csdn.net/detail/fx_odyssey/9838235

    下载libtiff源码后,新建win32空工程,将所有源码文件导入工程。新建cpp文件,键入代码:

#include <iostream>
#include "tiffio.h"
using namespace std;

int main()
{
    TIFF *tifFile = TIFFOpen("p.tif", "r"); //提前在工程目录下放入 p.tif 的文件,“r”表示读写权限为“读”
    int width, height;
    TIFFGetField(tifFile, TIFFTAG_IMAGEWIDTH, &width);
    TIFFGetField(tifFile, TIFFTAG_IMAGELENGTH, &height);
    int pixelCount = width * height;
    unsigned int *image = new unsigned int[pixelCount];
    TIFFReadRGBAImage(tifFile, width, height, image, 1);
    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];
    for (int i = 0; i < pixelCount; i++)
    {
        R[i] = 0x00;
        G[i] = 0x00;
        B[i] = 0x00;
        A[i] = 0x00;
    }
    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;

    system("pause");
    return 0;
}

    注意:此资源适用于vs2008,vs2015会提示错误,不过下载其他的libtiff库此代码是一样适用的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值