如何将PNM文件连接为数组

How can I concatenate PNM files as arrays?

本文关键字:连接 数组 文件 PNM      更新时间:2023-10-16

我想写一个函数,从左到右连接两个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);
    }
}

(未测试或编译)