HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Image loader/writer design (no API, pure C++ on Windows)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
loaderimagepurewriterdesignwindowsapi

Problem

I have an image loader, now only for bitmaps. I'm a little confused because I want to split my code to different classes, one for writing a BMP data to a file (WRITER), one for loading BMP data from file, or from pre-generated dataset (1) and storing it in its private members.( (1) like a noise function saved to an array).

Currently I have the 2 class for this. The header:

```
namespace PND
{

inline void out(const ::std::string); //standard output log
inline void err(const ::std::string); //error log

bool file_exists(::std::string m_filename) //this could be used in both classes
{
::std::ifstream infile(m_filename+".bmp");
return infile.good();
}

class BMP
{
public:
BMP();
BMP(const BMP&);//copy csrt
~BMP();
BMP& operator= (BMP other); //ass. operator

bool loadData(::std::string filename);//from file, filename is the file's filename
bool loadData(::std::string filename,const int width, const int height, const unsigned char* data, const unsigned int length);//from array, filename is user defined
//they are simple pixel data accessors with out of bounds handling
unsigned char getData_B(const unsigned int x, const unsigned int y);
unsigned char getData_G(const unsigned int x, const unsigned int y);
unsigned char getData_R(const unsigned int x, const unsigned int y);

unsigned int* getSize() { return m_size; }; //some getter
::std::string getName() { return m_filename; };

private:

void swap(BMP& other);
void freeData();
//the bitmap data:
BITMAPINFOHEADER m_infoheader;
BITMAPFILEHEADER m_fileheader;
::std::string m_filename;
unsigned char* m_data;
unsigned int m_size[2];
long __m_size; //it is the length of the m_data array (num of pixels * 3)
};

class WRITER
{
public:
bool writeData(::std::string filename, const int

Solution

The only standard ways to access private data from another class are:

  • Getters



  • Friend class



If you want to make a friend class, just do something like this:

class Writer ;

class BMP
{
    //...
private:
friend class Writer ;
}


Now the Writer class can access all of BMP's data fields.

I would personally suggest using getters though. You could have getters for the header information and use operator [] for accessing any arrays.

unsigned char& BMP::operator [] (const size_t i) {
    return m_data [i] ;
}

const unsigned char& BMP::operator [] (const size_t i) const {
    return m_data [i] ;
}

Code Snippets

class Writer ;

class BMP
{
    //...
private:
friend class Writer ;
}
unsigned char& BMP::operator [] (const size_t i) {
    return m_data [i] ;
}

const unsigned char& BMP::operator [] (const size_t i) const {
    return m_data [i] ;
}

Context

StackExchange Code Review Q#37365, answer score: 3

Revisions (0)

No revisions yet.