Miss Rainbowdash
Miss Rainbowdash

Reputation: 205

How can I concatenate PNM files as arrays?

I want to write a function that concatenates (left to right) two PNM (P6) files that are stored pixel-by-pixel in Image classes. I have the function set up as follows:

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

So given that input1 and input2 have the same height, they should be placed in a new output alongside each other. Are there any straightforward ways of doing this in C++? No need to write working code--I'm just trying to come up with ideas.

EDIT: My image header file, as requested:

#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

Upvotes: 1

Views: 285

Answers (1)

clstrfsck
clstrfsck

Reputation: 14829

Not really an answer, but too long for a comment.

Would you need something like:

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

(not tested or compiled)

Upvotes: 1

Related Questions