gpt4 book ai didi

c++ - LibTIFF 在 C++ 中读写 RGBA 图像

转载 作者:行者123 更新时间:2023-12-02 10:24:31 24 4
gpt4 key购买 nike

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

关于c++ - LibTIFF 在 C++ 中读写 RGBA 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48884728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com