LibTIFF用c ++读写RGBA图像

时间:2018-02-20 11:53:09

标签: c++ libtiff

我是C ++的新手,我使用LibTIFF来读写RGBA图像。但是,写入的图像与图像读取不同。 请问有什么人让我知道我在哪里弄错了并纠正了我的代码 ?我的代码如下:我已经遵​​循了许多教程,但无法解决问题。

TIFF *target = TIFFOpen("im16temp.tif", "r");

uint32 width;
uint32 height;
uint32 *rasterTarget;
uint32 BPP;


TIFFGetField(target, TIFFTAG_IMAGEWIDTH, &width);       // uint32 width;
TIFFGetField(target, TIFFTAG_IMAGELENGTH, &height);    // uint32 height;
TIFFGetField(target, TIFFTAG_BITSPERSAMPLE, &BPP);
uint32 npixels = width * height;

int  sampleperpixel = 4;

rasterTarget = (uint32 *) _TIFFmalloc(npixels * sizeof(uint32) * 2);

TIFFReadRGBAImageOriented(target, width, height, rasterTarget,
ORIENTATION_TOPLEFT, 0);

TIFF *outnew= TIFFOpen("new.tif", "w");




TIFFSetField (outnew, TIFFTAG_IMAGEWIDTH, width);  // set the width of the image

TIFFSetField(outnew, TIFFTAG_IMAGELENGTH, height);    // set the height of the image
TIFFSetField(outnew, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel);   // set number of channels per pixel
TIFFSetField(outnew, TIFFTAG_BITSPERSAMPLE, 16);    // set the size of the channels
TIFFSetField(outnew, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);    // set the origin of the image.
//   Some other essential fields to set that you do not have to understand for now.
TIFFSetField(outnew, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(outnew, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
tsize_t linebytes = sampleperpixel * width * 2 ;     // length in memory of one row of pixel in the image.

cout<< "Lien of linebytes : "<<linebytes<<endl;



unsigned char *buf = NULL;        // buffer used to store the row of pixel information for writing to file
//    Allocating memory to store the pixels of current row
if (TIFFScanlineSize(outnew)!=linebytes)
    buf =(unsigned char *)_TIFFmalloc(linebytes);
else
    buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(outnew));

// We set the strip size of the file to be size of one row of pixels
TIFFSetField(outnew, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(outnew, linebytes));



//Now writing image to the file one strip at a time
for (uint32 row = 0; row <  height; row++)
{

    //memcpy(buf, &rasterTargetinput[( height1-row-1)*linebytes], linebytes);    // check the index here, and figure out why not using h*linebytes
    memcpy(buf, &rasterTarget[row*linebytes], linebytes);    // check the index here, and figure out why not using h*linebytes
    //memcpy(buf, &rasterTarget[(height-row-1)*linebytes], linebytes);
    if (TIFFWriteScanline(outnew, buf, row, 0) < 0)
    break;
}

这是输入和输出图像的tiffdump的输出。

输出图片:

Magic: 0x4d4d <big-endian> Version: 0x2a <ClassicTIFF>
Directory 0: offset 392 (0x188) next 0 (0)
ImageWidth (256) SHORT (3) 1<8>
ImageLength (257) SHORT (3) 1<8>
BitsPerSample (258) SHORT (3) 3<16 16 16>
Compression (259) SHORT (3) 1<1>
Photometric (262) SHORT (3) 1<2>
StripOffsets (273) LONG (4) 1<8>
Orientation (274) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<3>
RowsPerStrip (278) SHORT (3) 1<48>
StripByteCounts (279) LONG (4) 1<384>
PlanarConfig (284) SHORT (3) 1<1>
SampleFormat (339) SHORT (3) 3<1 1 1>

输入图片:

Magic: 0x4d4d <big-endian> Version: 0x2a <ClassicTIFF>
Directory 0: offset 392 (0x188) next 0 (0)
ImageWidth (256) SHORT (3) 1<8>
ImageLength (257) SHORT (3) 1<8>
BitsPerSample (258) SHORT (3) 3<16 16 16>
Compression (259) SHORT (3) 1<1>
Photometric (262) SHORT (3) 1<2>
StripOffsets (273) LONG (4) 1<8>
Orientation (274) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<3>
RowsPerStrip (278) SHORT (3) 1<48>
StripByteCounts (279) LONG (4) 1<384>
PlanarConfig (284) SHORT (3) 1<1>
SampleFormat (339) SHORT (3) 3<1 1 1>

1 个答案:

答案 0 :(得分:1)

当为TIFFTAG_SAMPLESPERPIXEL设置的值大于3(在您的情况下,sampleperpixel = 4)时,您可能还需要将TIFFTAG_EXTRASAMPLES字段设置为适当的值。

请参阅此处: https://www.awaresystems.be/imaging/tiff/tifftags/samplesperpixel.html