I'm trying my hand at CS50's Filter and I'm at the blurring of pixels part. I'm able to access colors of a pixel with
image[row][pixel].rgbtRed
image[row][pixel].rgbtGreen
image[row][pixel].rgbtBlue
I want to be able to call a function that calculations the average of the surrounding pixels and pass in the color that I want the average of. Is there some sort of placeholder so that I can access a particular element/attribute of the struct? (Not sure of the proper name of it sorry).
I'm still very new and tried putting it between brackets and so on but nothing worked.
Here's the function where I try to get the value of the passed on color.
float calcAverage(int height, int width, RGBTRIPLE image[height][width], int row, int pixel, string color)
{
float sum = image[row][pixel].color + image[row][pixel - 1].color;
return 0;
}
And this is how I call the function.
redAverage = calcAverage(height, width, image, row, pixel, "rgbtRed");
Right now my .color plan doesn't work because know he's looking for an attribute called color. This is the error I get
error: no member named 'color' in 'RGBTRIPLE'
float sum = image[row][pixel].color + image[row][pixel - 1].color;
I kept the sum short for testing purposes. Thanks in advance, I'm starting to think this isn't possible and I should leave it. Once again sorry if I use the wrong terminology for what I'm looking for.
You can't use a string variable as a substitute for a member name. You would need to check the string's value and select the field based on that.
float calcAverage(int height, int width, RGBTRIPLE image[height][width],
int row, int pixel, string color)
{
if (!strcmp(color, "rgbtRed")) {
return image[row][pixel].rgbtRed + image[row][pixel - 1].rgbtRed;
} else if (!strcmp(color, "rgbtGreen")) {
return image[row][pixel].rgbtGreen + image[row][pixel - 1].rgbtGreen;
} else if (!strcmp(color, "rgbtBlue")) {
return image[row][pixel].rgbtBlue + image[row][pixel - 1].rgbtBlue;
} else {
// invalid color
return 0;
}
}