使用libtiff写RGB彩色tiff图

前记:前前后后一年时间,有时间就扒资料,终于弄出来了。这个程序是读取一个彩色tiff图,将其像素提取出来,赋给新建的tiff文件。拷贝运行此程序前,先配置好libtiff环境(网上有很多资源,也可以下载我收藏的的源码文件:http://download.csdn.net/download/fx_odyssey/9838235,直接拷贝到工程目录下。),另外将lena.tif彩色文件放置在工程目录下。

#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include "tiffio.h"

using namespace std;

void CreatTiffFile(const char *FileName, unsigned char *Buf, uint32 width, uint32 height);

int main()
{
    TIFF *tifFile = TIFFOpen("lena.tif", "r");  //提前在工程目录下放入 lena.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;

    uint32 pixel = height * width * 3;
    BYTE *pic = new BYTE[pixel];
    for (uint32 i = 0; i < pixelCount; i++)
    {
        pic[i*3] = R[i];
        pic[i*3+1] = G[i];
        pic[i*3+2] = B[i];
    }
    delete R;
    delete G;
    delete B;
    delete A;

    const char *fileName = "test.tif";
    CreatTiffFile(fileName, pic, width, height);
    return 0;
}

void CreatTiffFile(const char *FileName, unsigned char *Buf, uint32 width, uint32 height)
{
    TIFF *imageTest = TIFFOpen(FileName, "w");
    if (imageTest == NULL) {
        printf("Could not open output.tif for writing\n");
        cout << "open is failed.\n";
        system("pause");
        exit(1);
    }
    TIFFSetField(imageTest, TIFFTAG_IMAGEWIDTH, width);
    TIFFSetField(imageTest, TIFFTAG_IMAGELENGTH, height);
    TIFFSetField(imageTest, TIFFTAG_BITSPERSAMPLE, 8);// bits per channel (sample) 每个通道(样本)
    TIFFSetField(imageTest, TIFFTAG_SAMPLESPERPIXEL, 3);// samples per pixel 每像素采样
    TIFFSetField(imageTest, TIFFTAG_ROWSPERSTRIP, height);           // rows per strip of data 每条数据行(写入像素的高度限制)
    //TIFFSetField(imageTest, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
    TIFFSetField(imageTest, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    TIFFSetField(imageTest, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
    TIFFSetField(imageTest, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(imageTest, TIFFTAG_XRESOLUTION, 600.0);
    TIFFSetField(imageTest, TIFFTAG_YRESOLUTION, 600.0);
    TIFFSetField(imageTest, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
    //Write the information to the file
    TIFFWriteEncodedStrip(imageTest, 0, Buf, width * height * 3);

    TIFFClose(imageTest);
    //return true;
}

如果是想写灰度图,将TIFFSetField(imageTest, TIFFTAG_SAMPLESPERPIXEL, 3);中3改为1,Buf的数据大小改为width*height即可。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值