0

sorry if the question is confusing, but i completed the whodunit problem in pset3 and my doubt was that in the following code:

    // iterate over pixels in scanline
    for (int j = 0; j < bi.biWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        if (triple.rgbtBlue < 0xff && triple.rgbtGreen < 0xff && triple.rgbtRed > 0xfd)
        {
            triple.rgbtBlue = 0xff;
            triple.rgbtGreen = 0xff;
            triple.rgbtRed = 0xff;
        } 

        // write RGB triple to outfile
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    }

I understand that the line fread(&triple, sizeof(RGBTRIPLE), 1, inptr); reads 3 bytes (or a pixel) of data from inputr and stores it in triple, which is defined as:

    typedef struct
    {
        BYTE rgbtBlue;
        BYTE rgbtGreen;
        BYTE rgbtRed;
    } __attribute__((__packed__))
    RGBTRIPLE;

But how does triple assign 1 byte each, from the 3 bytes, to rgbtBlue, rgbtGreen, and rgbtRed automatically when we are only providing the address to the buffer, &triple. I still know little about structures but to store data in a struct weren't we supposed to assign the value to each member individually?

Also, while trying to use debug50 to find the answer to my question I found that values of rgbtBlue, rgbtGreen and rgbtRed were stored as: <code>Triple</code> values

The first part defines the intensity of the colour, from 0 to 255, but can you please elaborate on the second part in single quotation mark ('\377', 'x')?

Note: The values in the picture are only from one of the iteration of for loop and the some had something like '\307' instead of 'x' for blue and green too.

Thank you for your help :)


1

When a struct is stored in memory, the data for each struct element is stored in sequence. That means that when a struct's data is read, the data is simply read into each element in the same order that it was stored. Individual elements can be selected out by reading them from their position in the struct. It's all about the location of each element in the struct, nothing more complicated than that.

As you might have guessed, debug50 is displaying both the hex values and the ASCII chars that correspond. For 120, it's simple, 'x'. But, 255 is an extended ASCII char, the non-break space, or &nbsp. (Google ASCII 255).

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Hey, thanks for the quick explanation. On observing a few more iterations, the characters debug50 can show it shows(like 'x') but replaces the special chars in the extended ASCII with '\number'(like '\307'). What does the backslash number denote? Is it just a simple convention for foreign characters? Couldn't find it on google. – Akarsh Oct 31 '19 at 19:55
  • Basically, it's saying it's a non-printable and the backslash indicates the number is the decimal ASCII code. If you want to know what it is, google "ASCII TABLE" or "ASCII 307" Above 255, you're looking at unicode. – Cliff B Oct 31 '19 at 20:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .