I'm new to C++ and I have a question. I have an array of float* type. I also have class Color* buffer. I want to store the array's components into the buffer.(or make the buffer's pointer look at the same memory space as the array's but I'm not really familiar yet with C++ and I still cant understand pointers clearly) Is this possible? If not is there any other way to make it somehow possible?
Edit: The Color class is a class which contains all the methods to modify a PPM file's data. I need to store this data into a float array. Then I have to modify the data. But then there is a Color* buffer initialized and I can't understand how to move the data from the array into this Color* buffer in order to use it for all the methods of this Image class:
class Image
{
public:
protected:
Color * buffer; //! Holds the image data.
unsigned int width, //! The width of the image (in pixels)
height; //! The height of the image (in pixels)
public:
// metric accessors
/*! Returns the width of the image
*/
const unsigned int getWidth() const { return width; }
/*! Returns the height of the image
*/
const unsigned int getHeight() const { return height; }
// data accessors
/*! Obtains a pointer to the internal data.
*
* This is NOT a copy of the internal image data, but rather a pointer
* to the internally allocated space, so DO NOT attempt to delete the pointer.
*/
Color * getRawDataPtr();
/*! Obtains the color of the image at location (x,y).
*
* The method should do any necessary bounds checking.
*
* \param x is the (zero-based) horizontal index of the pixel to get.
* \param y is the (zero-based) vertical index of the pixel to get.
*
* \return The color of the (x,y) pixel as a Color object. Returns a black (0,0,0) color in case of an out-of-bounds x,y pair.
*/
Color getPixel(unsigned int x, unsigned int y) const;
// data mutators
/*! Sets the RGB values for an (x,y) pixel.
*
* The method should perform any necessary bounds checking.
*
* \param x is the (zero-based) horizontal index of the pixel to set.
* \param y is the (zero-based) vertical index of the pixel to set.
* \param value is the new color for the (x,y) pixel.
*/
void setPixel(unsigned int x, unsigned int y, Color & value);
/*! Copies the image data from an external raw buffer to the internal image buffer.
*
* The member function ASSUMES that the input buffer is of a size compatible with the internal storage of the
* Image object and that the data buffer has been already allocated. If the image buffer is not allocated or the
* width or height of the image are 0, the method should exit immediately.
*
* \param data_ptr is the reference to the preallocated buffer from where to copy the data to the Image object.
*/
void setData(const Color * & data_ptr);
// constructors and destructor
/*! Default constructor.
*
* By default, the dimensions of the image should be zero and the buffer must be set to nullptr.
*/
Image() {};
/*! Constructor with width and height specification.
*
* \param width is the desired width of the new image.
* \param height is the desired height of the new image.
*/
Image(unsigned int width, unsigned int height);
/*! Constructor with data initialization.
*
* \param width is the desired width of the new image.
* \param height is the desired height of the new image.
* \param data_ptr is the source of the data to copy to the internal image buffer.
*
* \see setData
*/
Image(unsigned int width, unsigned int height, const Color * data_ptr);
/*! Copy constructor.
*
* \param src is the source image to replicate in this object.
*/
Image(const Image &src);
/*! The Image destructor.
*/
~Image();
/*! Copy assignment operator.
*
* \param right is the source image.
*/
Image & operator = (const Image & right);
/*!
* Loads the image data from the specified file, if the extension of the filename matches the format string.
*
* Only the "ppm" extension is supported for now. The extension comparison should be case-insensitive. If the
* Image object is initialized, its contents are wiped out before initializing it to the width, height and data
* read from the file.
*
* \param filename is the string of the file to read the image data from.
* \param format specifies the file format according to which the image data should be decoded from the file.
* Only the "ppm" format is a valid format string for now.
*
* \return true if the loading completes successfully, false otherwise.
*/
bool load(const std::string & filename, const std::string & format);
/*!
* Stores the image data to the specified file, if the extension of the filename matches the format string.
*
* Only the "ppm" extension is supported for now. The extension comparison should be case-insensitive. If the
* Image object is not initialized, the method should return immediately with a false return value.
*
* \param filename is the string of the file to write the image data to.
* \param format specifies the file format according to which the image data should be encoded to the file.
* Only the "ppm" format is a valid format string for now.
*
* \return true if the save operation completes successfully, false otherwise.
*/
bool save(const std::string & filename, const std::string & format);
};