I'm supposed to change the RGB color to gray by getting the average of the 3 and then assigning that average to each of the RBG colors(red, blue, green) however it isn't working.
First I'll post my code for the grayscale part:
#include "helpers.h"
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
RGBTRIPLE rgbt = image[i][j];
BYTE average = (rgbt.rgbtBlue/3)+(rgbt.rgbtGreen/3)+(rgbt.rgbtRed);
*rgbt.rgbtBlue = *average;
*rgbt.rgbtGreen = *average;
*rgbt.rgbtRed = *average;
}
}
return;
}
And since its important, here where the RGBT values are mentionned: (in another file, mentionned in the header for helpers.h, called bmp.h):
/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red, green, and blue.
*
* Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
*/
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
The error message is as follows:
indirection requires pointer operand ('int' invalid)
Thanks for your help (and I know know that cs50 isn't the best course but the Harvard diploma helps.).
I think you want this:
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
RGBTRIPLE rgbt = image[i][j];
BYTE average = (rgbt.rgbtBlue + rgbt.rgbtGreen + rgbt.rgbtRed) / 3;
rgbt.rgbtBlue = average;
rgbt.rgbtGreen = average;
rgbt.rgbtRed = average;
image[i][j] = rgbt;
}
}
return;
}
Hint: you can simplyfy this by getting rid of the rgbt
variable. You only need one line of code inside the inner for loop.
Problems in your original code:
This is wrong:
BYTE average = (rgbt.rgbtBlue/3)+(rgbt.rgbtGreen/3)+(rgbt.rgbtRed);
because you didn't divide rgbt.rgbtRed
by 3, and anyway you should first add all values and then divide the whole thing by 3 which this will prevent roundoff errors:
BYTE average = (rgbt.rgbtBlue + rgbt.rgbtGreen + rgbt.rgbtRed) / 3;
This is wrong, you use *
to dereference something that is not a pointer:
*rgbt.rgbtBlue = *average;
You simply want:
rgbt.rgbtBlue = average;
And last but not least you still need to modify the original value in the image by adding this:
image[i][j] = rgbt;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.