Reputation: 1124
We were given some code for an assignment. I'm having some trouble determining why the get is in the .h file and the set is in the .cpp file. When I call theTexMap.GetRgbPixel(x, y, r, g, b);
in my texture class it isn't able to find the GetRgbPixel function in the .h file (obviously). Probably need to add the get into the RgbImage.cpp? Someone have a moment to explain? It looks like it is all fully built in the .h for the getter so I'm not sure why I would have to add this function. To what extent do I need to add the GetRgbPixel function when alot of it is already in the .h?
/*********************************************************************
* SetRgbPixel routines allow changing the contents of the RgbImage. *
*********************************************************************/
void RgbImage::SetRgbPixelf( long row, long col, double red, double green, double blue )
{
SetRgbPixelc( row, col, doubleToUnsignedChar(red),
doubleToUnsignedChar(green),
doubleToUnsignedChar(blue) );
}
void RgbImage::SetRgbPixelc( long row, long col,
unsigned char red, unsigned char green, unsigned char blue )
{
assert ( row<NumRows && col<NumCols );
unsigned char* thePixel = GetRgbPixel( row, col );
*(thePixel++) = red;
*(thePixel++) = green;
*(thePixel) = blue;
}
RbgImage.h File code.
// Returned value points to three "unsigned char" values for R,G,B
inline const unsigned char* RgbImage::GetRgbPixel( long row, long col ) const
{
assert ( row<NumRows && col<NumCols );
const unsigned char* ret = ImagePtr;
long i = row*GetNumBytesPerRow() + 3*col;
ret += i;
return ret;
}
inline unsigned char* RgbImage::GetRgbPixel( long row, long col )
{
assert ( row<NumRows && col<NumCols );
unsigned char* ret = ImagePtr;
long i = row*GetNumBytesPerRow() + 3*col;
ret += i;
return ret;
}
inline void RgbImage::GetRgbPixel( long row, long col, float* red, float* green, float* blue ) const
{
assert ( row<NumRows && col<NumCols );
const unsigned char* thePixel = GetRgbPixel( row, col );
const float f = 1.0f/255.0f;
*red = f*(float)(*(thePixel++));
*green = f*(float)(*(thePixel++));
*blue = f*(float)(*thePixel);
}
inline void RgbImage::GetRgbPixel( long row, long col, double* red, double* green, double* blue ) const
{
assert ( row<NumRows && col<NumCols );
const unsigned char* thePixel = GetRgbPixel( row, col );
const double f = 1.0/255.0;
*red = f*(double)(*(thePixel++));
*green = f*(double)(*(thePixel++));
*blue = f*(double)(*thePixel);
}
Upvotes: 0
Views: 160
Reputation: 1833
Inline functions should be defined in the header so that is not the issue. inline
tells the compiler that wherever the function is called, it will instead replace the call to the function with the actual code in the function. It can be used as a speed optimization, but inline
is really only a hint to the compiler. Ultimately it's up to the compiler to decide what it will do.
Your issue is probably that GetRgbPixel
takes pointers for the red
, green
, and blue
parameters since that is where it returns the values.
You will want to do something like this instead:
float r, g, b;
theTexMap.GetRgbPixel(x, y, &r, &g, &b);
The &
operator gets the address of the variable which converts it to a pointer and allows the GetRgbPixel
function to return the values to those parameters.
Upvotes: 1