gpt4 book ai didi

c++ - 如何将 PNM 文件连接成数组?

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:56 25 4
gpt4 key购买 nike

我想编写一个函数来连接(从左到右)两个 PNM (P6) 文件,这些文件逐像素存储在 Image 类中。我的功能设置如下:

void LRConcatenate()
{
Image* input1 = GetInput();
Image* input2 = GetInput2();
Image* output = GetOutput();

if (input1->GetY() == input2->GetY())
{
output->ResetSize(input1->GetX()+input2->GetX(), input1->GetY());
// rest of logic goes here
}
}

鉴于 input1input2 具有相同的高度,它们应该并排放置在新的 output 中。在 C++ 中有没有直接的方法可以做到这一点?无需编写工作代码——我只是想出点子。

编辑:我的图像头文件,按要求:

#ifndef IPIXEL_H
#define IPIXEL_H

struct PixelStruct
{
unsigned char red;
unsigned char green;
unsigned char blue;
};

#endif

#ifndef IMAGE_H
#define IMAGE_H
class Image
{
private:
int x;
int y;
PixelStruct *data;

public:
Image(void); /* Default constructor */
Image(int width, int height, PixelStruct* data); /* Parameterized constructor */
Image(const Image& img); /* Copy constructor */
~Image(void); /* Destructor */
void ResetSize(int width, int height);
int GetX();
int GetY();
PixelStruct* GetData();
void SetData(PixelStruct *data);
};
#endif

最佳答案

不是真正的答案,但评论太长了。

你需要这样的东西吗:

if (input1->GetY() == input2->GetY())
{
const size_t width1 = input1->GetX();
const size_t width2 = input2->GetX();
const size_t width12 = width1 + width2;
const size_t height = input1->GetY();

output->ResetSize(width12, height);

for (int y = 0; y < height; ++y)
{
PixelStruct out_row = output->GetData() + (y * width12);
const PixelStruct *data1_row = input1->GetData() + (y * width1);
std::copy(data1_row, data1_row + width1, out_row);
const PixelStruct *data2_row = input2->GetData() + (y * width2);
std::copy(data2_row, data2_row + width2, out_row + width1);
}
}

(未经测试或编译)

关于c++ - 如何将 PNM 文件连接成数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23926600/

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