【问题标题】:LibTIFF reading and writing RGBA image in c++LibTIFF在c++中读写RGBA图像
【发布时间】:2018-02-20 11:53:08
【问题描述】:

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

【问题讨论】:

  • 我认为更喜欢"rb""wb"TIFFOpen()
  • 尝试tiffdump image.tif 查看输入和输出文件之间的差异。
  • 试过 "rb" 和 "wb" 没有任何改变:/ 并且 tiffdump 显示两个图像属性相同。
  • tiffdump 呢?
  • 呃!不要将代码和输出粘贴到无法格式化的 cmets 中。而是在您的问题下单击edit 并将其粘贴到那里。我们的想法是在您的输入和输出文件上运行tiffdump,以便查看差异并找出问题所在。

标签: c++ libtiff


【解决方案1】:

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

请看这里: https://www.awaresystems.be/imaging/tiff/tifftags/samplesperpixel.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多