T
Torsten Wiebesiek
Hi folks,
currently I'm writing image classes for easier handling of Intel's IPP library.
My idea is to have to different classes. One class that represents a complete
image and deals with all the memeory management stuff, and another class, that
is only a tile of the image. Let's call the first MemoryImage, and the second
ReferenceImage.
My code works well for creating non const and const reference images to a non
const memory image. Unfortunately, it doesn't work for const memory images.
The following code illustrates my problem. It's neither stylistically nor
functionally perfect (in fact it's neglecting the identical byte offsets
between consecutive images lines, even though the image widths might be
different.
class MemImage {
public:
// Create image.
MemImage(int width, int height) : m_data(0) {
// check dimensions and allocate aligned memory
}
// Get width of image.
int getWidth() const { return m_width; }
// Get height of image.
int getHeight() const { return m_height; }
// Get pointer to image data.
unsigned char* getData() { return m_data; }
// Get const pointer to image data.
const unsigned char* getData() const { return m_data; }
protected:
unsigned char * m_data;
int m_width;
int m_height;
}; // class MemImage
class RefImage {
public:
RefImage(MemImage &image, int x, int y, int width, int height)
: m_data(image.getData()+y*image.getWidth()+x), m_width(width), m_height(height)
{}
// Get pointer to image data.
unsigned char* getData() { return m_data; }
// Get const pointer to image data.
const unsigned char* getData() const { return m_data; }
protected:
unsigned char *m_data;
int m_width;
int m_height;
}; // class RefImage
With the example code above, its possible to do:
void f()
{
MemImage m(400,300);
RefImage r(m, 40, 30, 200, 150);
const RefImge c(m, 40, 30, 200, 150);
}
It's also possible to change image data of m through r.getData()
The code breaks with:
void g()
{
const MemImage m(400,300);
const RefImge c(m, 40, 30, 200, 150);
}
Changing the constructor of RefImage to
RefImage(const MemImage &image, int x, int y, int width, int height)
: m_data(image.getData()+y*image.getWidth()+x), m_width(width), m_height(height)
{}
fails due to the initialization of unsigned char m_data with a const unsigned char.
Changing the member m_data of RefImage to const unsigned char, the constructor works,
but it's no longer possible to have write access to non const MemImages via a RefImage.
Can anyone break this vicious circle?
Thanks in advance,
Torsten
currently I'm writing image classes for easier handling of Intel's IPP library.
My idea is to have to different classes. One class that represents a complete
image and deals with all the memeory management stuff, and another class, that
is only a tile of the image. Let's call the first MemoryImage, and the second
ReferenceImage.
My code works well for creating non const and const reference images to a non
const memory image. Unfortunately, it doesn't work for const memory images.
The following code illustrates my problem. It's neither stylistically nor
functionally perfect (in fact it's neglecting the identical byte offsets
between consecutive images lines, even though the image widths might be
different.
class MemImage {
public:
// Create image.
MemImage(int width, int height) : m_data(0) {
// check dimensions and allocate aligned memory
}
// Get width of image.
int getWidth() const { return m_width; }
// Get height of image.
int getHeight() const { return m_height; }
// Get pointer to image data.
unsigned char* getData() { return m_data; }
// Get const pointer to image data.
const unsigned char* getData() const { return m_data; }
protected:
unsigned char * m_data;
int m_width;
int m_height;
}; // class MemImage
class RefImage {
public:
RefImage(MemImage &image, int x, int y, int width, int height)
: m_data(image.getData()+y*image.getWidth()+x), m_width(width), m_height(height)
{}
// Get pointer to image data.
unsigned char* getData() { return m_data; }
// Get const pointer to image data.
const unsigned char* getData() const { return m_data; }
protected:
unsigned char *m_data;
int m_width;
int m_height;
}; // class RefImage
With the example code above, its possible to do:
void f()
{
MemImage m(400,300);
RefImage r(m, 40, 30, 200, 150);
const RefImge c(m, 40, 30, 200, 150);
}
It's also possible to change image data of m through r.getData()
The code breaks with:
void g()
{
const MemImage m(400,300);
const RefImge c(m, 40, 30, 200, 150);
}
Changing the constructor of RefImage to
RefImage(const MemImage &image, int x, int y, int width, int height)
: m_data(image.getData()+y*image.getWidth()+x), m_width(width), m_height(height)
{}
fails due to the initialization of unsigned char m_data with a const unsigned char.
Changing the member m_data of RefImage to const unsigned char, the constructor works,
but it's no longer possible to have write access to non const MemImages via a RefImage.
Can anyone break this vicious circle?
Thanks in advance,
Torsten