libtiff读写C 中的RGBA图像
LibTIFF reading and writing RGBA image in c++
我是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++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 将图像添加到资源文件夹UWP C++
- 彩色图像的卤化物处理平均值
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 重新定位图像时如何前进到下一个内存块
- 如何使用按钮更新GTK3图像以使用C++从相机捕获图片
- libtiff读写C 中的RGBA图像
- 使用 cuda 将通道与 RGBA 图像分离(无法显示完整图像)
- 加载图像到RGBA数组c++
- 如何从RGBA格式保存图像的缓冲区中创建opencv中的图像
- OpenGL转换RGBA字节像素到图像整数数组