3

The code below spits the following error when trying to compile:

invalid operands to binary expression ('int' and 'RGBTRIPLE')

Basically, I don't know how to properly multiply temp[i][j] to a factor.

I tried typecasting like 2 * int(temp[i][j]) for instance, but that does not work. How do I go about this one? What do I need to understand more about types in this case?

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j].rgbtRed   = image[i][j].rgbtRed;
            temp[i][j].rgbtGreen = image[i][j].rgbtGreen;
            temp[i][j].rgbtBlue  = image[i][j].rgbtBlue;
        }
    }

    int red_gx, green_gx, blue_gx, red_gy, green_gy, blue_gy;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            // Top Left Corner
            if (i == 0 && j == 0)
            {
                red_gx   = 0;
                green_gx = 0;
                blue_gx  = 0;

                red_gy   = 0;
                green_gy = 0;
                blue_gy  = 0;
            }

            //
            // <-- some other elif statements here in between
            //

            // Any other pixel
            else
            {
                red_gx   = (-1 * (temp[i - 1][j - 1])) + temp[i - 1][j + 1] + (-2 * (temp[i][j - 1])) + (2 * (temp[i][j + 1])) + (-1 * (temp[i + 1][j - 1])) + (temp[i + 1][j + 1]);
            //     green_gx = (-1 * temp[i - 1][j - 1]) + temp[i - 1][j + 1] + (-2 * temp[i][j - 1]) + (2 * temp[i][j + 1]) + (-1 * temp[i + 1][j - 1]) + temp[i + 1][j + 1];
            //     blue_gx  = (-1 * temp[i - 1][j - 1]) + temp[i - 1][j + 1] + (-2 * temp[i][j - 1]) + (2 * temp[i][j + 1]) + (-1 * temp[i + 1][j - 1]) + temp[i + 1][j + 1];

            //     red_gy   = (-1 * temp[i - 1][j - 1]) + (-2 * temp[i - 1][j]) + (-1 * temp[i - 1][j + 1]) + temp[i + 1][j - 1] + (2 * temp[i + 1][j]) + temp[i + 1][j + 1];
            //     green_gy = (-1 * temp[i - 1][j - 1]) + (-2 * temp[i - 1][j]) + (-1 * temp[i - 1][j + 1]) + temp[i + 1][j - 1] + (2 * temp[i + 1][j]) + temp[i + 1][j + 1];
            //     blue_gy  = (-1 * temp[i - 1][j - 1]) + (-2 * temp[i - 1][j]) + (-1 * temp[i - 1][j + 1]) + temp[i + 1][j - 1] + (2 * temp[i + 1][j]) + temp[i + 1][j + 1];
            }

            image[i][j].rgbtRed   = round(sqrt(pow(round(red_gx), 2) + pow(round(red_gy), 2)));
            image[i][j].rgbtGreen = round(sqrt(pow(round(green_gx), 2) + pow(round(green_gy), 2)));
            image[i][j].rgbtBlue  = round(sqrt(pow(round(blue_gx), 2) + pow(round(blue_gy), 2)));
        }
    }
    return;
}
  • 1
    The first loop in the code shows you what you need to do. – user3386109 Aug 30 '21 at 20:17
  • @user3386109 sorry, I can't seem to realize what you are pointing out. Might need to sleep it off first lol sun will rise soon – flamethrower10 Aug 30 '21 at 20:24
  • 1
    RGBTRIPLE is a struct with three members. There are no mathematical operations allowed on the struct as a whole. You must access the members individually, as shown in that first loop. Rest well :) – user3386109 Aug 30 '21 at 20:27
  • @user3386109 oh shoot i just realized my stoooopid mistake. the dot notation. lol. thanks! – flamethrower10 Aug 30 '21 at 20:29

3 Answers3

4

Here's the definition of RGBTRIPLE:

RGBTRIPLE structure (wingdi.h)

typedef struct tagRGBTRIPLE {
  BYTE rgbtBlue;
  BYTE rgbtGreen;
  BYTE rgbtRed;
} RGBTRIPLE, *PRGBTRIPLE, *NPRGBTRIPLE, *LPRGBTRIPLE;

It's a "struct", whose elements consist of 3 bytes. It should be pretty obvious that you can't just treat it as an integer. "2 * int(temp[i][j])" is clearly illegal.

What you CAN do is accumulate rgbtBlue, rgbtGreen and rgbtRed into a single 32-bit integer, if you wish. Perhaps something like:

unsigned int color = (temp[i][j].rgbtRed << 16) | (temp[i][j].rgbtGreen << 8) | (temp[i][j].rgbtBlue); 

You might also be able to define a union.

But why? Maybe your best bet might be to simply regard R, G and B as "separate"?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

you can combine them into one single 32-bit integer
do your math then put it back, there're macros in wingdi.h that can help you out

wingdi.h

#define GetRValue(c) ((BYTE)(c))
#define GetGValue(c) ((BYTE)(((WORD)(c))>>8))
#define GetBValue(c) ((BYTE)((c)>>16))
#define RGB(r,g,b) ((COLORREF)((BYTE)(r)|((BYTE)(g) << 8)|((BYTE)(b) << 16)))

you can go as follows:

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        // RGB macro takes 3 arguments of type BYTE(red, green ,blue)
        // and returns 32-bit integer

        int iColor  =  RGB(image[i][j].rgbtRed, image[i][j].rgbtGreen, image[i][j].rgbtBlue);

        // do whatever you want with iColor
        // then back to image

        image[i][j].rgbtRed      =   GetRValue(iColor);  //  retrieves red value(BYTE)
        image[i][j].rgbtGreen    =   GetGValue(iColor);  //  retrieves green
        image[i][j].rgbtBlue     =   GetBValue(iColor);  //  retrieves blue
    }
}
Errorist
  • 224
  • 2
  • 6
1
typedef union 
{
    struct
    {
        BYTE rgbtBlue;
        BYTE rgbtGreen;
        BYTE rgbtRed;
    };
    unsigned rawRGB;
} RGBTRIPLE, *PRGBTRIPLE, *NPRGBTRIPLE, *LPRGBTRIPLE;

then you can 2 * temp[i][j].rawRGB

BTW hiding pointers behind typedef is a very bad practice. Avoid as it usually leads to very difficult to find and debug errors.

0___________
  • 60,014
  • 4
  • 34
  • 74