Home > Back-end >  how do I handle an Array for a 2D Vector in an Object of another class?
how do I handle an Array for a 2D Vector in an Object of another class?

Time:10-27

I need a little help with an appointment of mine. My professor gave us this class (and a Color class that has RGB colors as float variables inside) now I have to implement the functions shown in the header.

#include "color.h"
#include <assert.h>

Color::Color()
{
    R = 255;
    G = 255;
    B = 255;
}

Color::Color( float r, float g, float b)
{
    R = r;
    G = g;
    B = b;
}

Color Color::operator*(const Color& c) const
{
    return Color(R * c.R, G * c.G, B * c.B );
}

Color Color::operator*(const float Factor) const
{
    return Color(R * Factor, G * Factor, B * Factor);
}

Color Color::operator (const Color& c) const
{
    return Color(R   c.R, G   c.G, B   c.B);
}

Color& Color::operator =(const Color& c)
{
    R  = c.R;
    G  = c.G;
    B  = c.B;
    return *this;
}

Header RGBImage

enter image description here

The Konstruktor should create a 2DImage memory to save width*height Pixel. (Dunno what the best solution here would be? Array of type Color or a Vector?) My first guess was this:

RGBImage class (i just got empty methodes)

#include "rgbimage.h"
#include "color.h"
#include "assert.h"

using namespace std;


RGBImage::RGBImage( unsigned int Width, unsigned int Height)
{
    m_Image = new Color[Width * Height];   // probably wrong?
    m_Width = Width;
    m_Height = Height;
}

RGBImage::~RGBImage()
{

}

void RGBImage::setPixelColor( unsigned int x, unsigned int y, const Color& c)
{
        if (x < width() && y < height())
        {
            // get offset of pixel in 2D array.
            const unsigned offset = (y * width())   x;
            m_Image[offset] = c;
        }
}


const Color& RGBImage::getPixelColor( unsigned int x, unsigned int y) const
{

    if (x < width() && y < height())
    {
        // get offset of pixel in 2D array.
        const unsigned offset = (y * width())   x;
        return m_Image[offset];
    }
    
}

unsigned int RGBImage::width() const
{
    return this->m_Width; 
}
unsigned int RGBImage::height() const
{
    return this->m_Height;
}

unsigned char RGBImage::convertColorChannel( float v)
{
    if (v < 0) {
        v = 0;
    }
    else if (v > 1) {
        v = 1;
    }

    int convertedColorChannel = v * 255;
    return convertedColorChannel;
}


bool RGBImage::saveToDisk( const char* Filename)
{
    // TODO: add your code
    return false; // dummy (remove)
}

afterward, I realized Color arrays are no variable of the Class RGBImage per definition of his Header so how can I save the Pixel in an RGBImage, or is it a viable option to continue this approach. If so how can I set the Color in a setter? tryed it with this.bildspeicher[x] didnt work...

I'm fairly new to Programming, and this is my first question on this platform, so sorry if I stated my problem poorly.

CodePudding user response:

RGB data is usually stored in a one-dimensional array, as is the case for RGBImage. The pixels are packed line-by-line, starting either from the bottom left, or the top-left of the image. The orientation should not affect the functions accessing individual rows of pixels, but will affect how the calling application handles the pixel data.

For accessing individual pixels, use this formula:

// ...

inline void setPixel(unsigned x, unsigned  y, const Color& clr)
{
    if (x < width() && y < height())  // ALWAYS crop!
    {
        // get offset of pixel in 2D array.
        const unsigned offset = (y * width())   x;
        m_image[offset] = clr;
    }
}

I've put the formula in its own line of code, but this is usually done as a one-liner.

The same formula can be used for reading pixels. Note that this formula assumes the lines of pixels have no alignment. Some bitmap fprmats do require 2 of 4 byte alignment of each line within the 2d array, in which case you'd multiply y by alignedWidth() instead of width()).

  •  Tags:  
  • c
  • Related