1

Imageクラスにピクセル単位で格納されている 2 つの PNM (P6) ファイルを (左から右に) 連結する関数を作成したいと考えています。次のように機能を設定しています。

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互いに並べて new に配置する必要があります。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
4

1 に答える 1

1

本当の答えではありませんが、コメントするには長すぎます。

次のようなものが必要ですか?

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);
    }
}

(テストもコンパイルもされていません)

于 2014-05-29T06:57:43.503 に答える