如何将PNM文件连接为数组
How can I concatenate PNM files as arrays?
我想写一个函数,从左到右连接两个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
}
}
因此,考虑到input1
和input2
具有相同的高度,它们应该并排放置在新的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 字符串数组与分隔符连接起来
- 需要关于如何将我的随机数数组连接到冒泡排序的帮助
- 如何将X 2维数组连接到一个三维阵列中
- qt如何连接QPushButton的2D数组并返回位置
- 连接数组,数组 1 内存消失C++
- 如何与字符串连接thar数组
- 如何连接 2D 数据数组
- 使用 << 或 >> 连接无符号字符数组
- 碰撞连接到通过STD ::数组
- 将字符数组连接在一起
- 在 C 语言中将 32 位 int 块连接在 16 个字符数组中
- 将字符数组解释为多个连接的数据类型
- 用C++连接数组中的数据
- 有没有直接的方法可以连接两个字符数组 (char*)
- 连接 2 个数组,一个字符串数组和一个数字数组
- 连接两个动态字符串数组
- C++字符串与字符数组连接
- 一个简单的 C++ 连接/拆分字符串和 char 数组中的整数
- 连接数组中的int
- 在cout语句中连接数组大小