libtiff读写C 中的RGBA图像

LibTIFF reading and writing RGBA image in c++

本文关键字:RGBA 图像 中的 读写 libtiff      更新时间:2023-10-16

我是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>

在设置tifftag_samplesperpixel的值时,大于3(sampleperpixel = 4(,您也可能需要将tifftag_extrasamples字段设置为适当的值。

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