Question

I'm relatively new to both C and bitwise operations and I'm having trouble with an assignment I've been given in my class. A majority of the code has been given to me, but I've been having issues figuring out a part pertaining to bitwise operations. Once I figure this part out, I'll be golden. I hope that someone can help!

Here is the excerpt from my assignment:

You will need to use 8 bytes of an image to hide one byte of information (remember that only LSBs of the cover image can be modified). You will use the rest of the 16 bytes of the cover image to embed 16 bits of b.size (two least significant bytes of the size field for the binary data), next 32 bytes of the cover will be used to embed the file extension for the payload file, and after that you will use 8*b.size bytes to embed the payload (b.data).

What this program is doing is stenography of an image, and I have to modify the least significant bits of the image read in using data from a file that I created. Like I said, all of the code for that is already written. I just can't figure out how to modify the LSBs. Any help would be greatly appreciated!!!

The functions I have to use for reformatting the LSBs are as follows:

byte getlsbs(byte *b);
void setlsbs(byte *b, byte b0);

This is what I've attempted thus far:

/* In main function */
b0 = getlsbs(&img.gray[0])

/* Passing arguments */
byte getlsbs(byte *b)
{
    byte b0;
    b0[0] = b >> 8;
    return b0;
}

I'm honestly at a complete loss. I've been working on this all night and I still barely have made headway.

Était-ce utile?

La solution

To set LSB of b to 1:

b |= 1;

To set LSB of b to 0:

b &= 0xFE;

Here is an idea how the functions could be implemented. This code is not tested.

byte getlsbs(byte *b)
{
    byte result = 0;
    for (int i = 0; i < 8; ++i)
    {
        result >>= 1;
        if (*b & 1)
            result |=  0x80;
        ++b;
    }
    return result;
}

void setlsbs(byte *b, byte b0)
{
    for (int i = 0; i < 8; ++i)
    {
        if (b0 & 1)
            *b |= 1;
        else
            *b &= 0xFE;
        ++b;
        b0 >>= 1;
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top