Jump to content

Hello, I'm trying to make a program in C that will encodes a message inside a PPM image colour code least signficant bits for a school project.

 

I convert each character to an int code (messageNumberCode[]) then extract the binary bits of this code and add them into the pixel colour structure. The binary code requires 6 bits, and I can only use 3 bits per pixel ie. lsb of R, lsb of G, lsb of B, so I put the first 3 bits into the first pixel, and the last 3 bits into the next pixel.

 

In the example I'm using "tzwqn" as my message.

rPixel is the psuedo random location of a pixel that will be altered.

 

My console output is as follows

t => 20 | z => 26 | w => 23 | q => 17 | n => 14 |
Number of Characters: 4rPixel: 13 | rPixel: 0 | rPixel: 6 | rPixel: 3 | rPixel: 12 | rPixel: 2 | rPixel: 14 | rPixel: 15 |
Writing LHS of 20 at pos 13. The RGB of this pixel is 010. Counter is 0. pos is 13
Writing RHS of 20 at pos 0. The RGB of this pixel is 100. Counter is 1. pos is 0
Writing LHS of 23 at pos 6. The RGB of this pixel is 010. Counter is 0. pos is 6
Writing RHS of 23 at pos 3. The RGB of this pixel is 111. Counter is 1. pos is 3
Writing LHS of 14 at pos 12. The RGB of this pixel is 100. Counter is 0. pos is 12
Writing RHS of 14 at pos 2. The RGB of this pixel is 110. Counter is 1. pos is 2
Writing LHS of 0 at pos 14. The RGB of this pixel is 000. Counter is 0. pos is 14
Writing RHS of 0 at pos 15. The RGB of this pixel is 000. Counter is 1. pos is 15

 

I am unsure as to why it is skipping every other character ie. instead of encoding "tzwqn" it encodes "twn". Below you will find the source code relevant to this:https://pastebin.com/7Wa1kF3z

If you understand what is going wrong and how to resolve the issue, please let me know. ?

Link to comment
https://linustechtips.com/topic/1038631-c-programming-help/
Share on other sites

Link to post
Share on other sites

  for (int i = 0; i < NumberOfCharacters * 2; i++) // <----------------------------
    {
 
        int pos = 0;
 
        //loop vertically through the array
        for (int a = 0; a < ppm->height; a++)
        {
            //loop horz through the array
            for (int j = 0; j < ppm->width; j++)
            {
 
                //if looking at a pixel selected psuedo randomly
                if (pos == randomPixelIndex[i])
                {
 
                    //if this is the first pixel of this char
                    if (counter == 0)
                    {
 
                        //RESET LSB OF EACH COLOURS
                        if (ppm->pixels[a][j].red % 2 != 0)
                        {
                            ppm->pixels[a][j].red--;
                        }
 
                        if (ppm->pixels[a][j].green % 2 != 0)
                        {
                            ppm->pixels[a][j].green--;
                        }
 
                        if (ppm->pixels[a][j].blue % 2 != 0)
                        {
                            ppm->pixels[a][j].blue--;
                        }
 
                        //obtain bit from messageNumberCode[i]
                         lsbR = ((1 << 1) - 1) & (messageNumberCode[i] >> (4 - 1));
                         lsbG = ((1 << 1) - 1) & (messageNumberCode[i] >> (5 - 1));
                         lsbB = ((1 << 1) - 1) & (messageNumberCode[i] >> (6 - 1));
 
                        //update pixel lsb values
                        ppm->pixels[a][j].red |= lsbR;
                        ppm->pixels[a][j].green |= lsbG;
                        ppm->pixels[a][j].blue |= lsbB;
 
 
                        printf("\nWriting LHS of %d at pos %d. The RGB of this pixel is %d%d%d. Counter is %d. pos is %d", messageNumberCode[i], randomPixelIndex[i], lsbR, lsbG, lsbB, counter, pos);
 
                        counter++;
                    }
                    else
                    {
                        //RESET LSB OF EACH COLOURS
                        if (ppm->pixels[a][j].red % 2 != 0)
                        {
                            ppm->pixels[a][j].red--;
                        }
 
                        if (ppm->pixels[a][j].green % 2 != 0)
                        {
                            ppm->pixels[a][j].green--;
                        }
 
                        if (ppm->pixels[a][j].blue % 2 != 0)
                        {
                            ppm->pixels[a][j].blue--;
                        }
 
                        //obtain bit from messageNumberCode[i]
                         lsbR = ((1 << 1) - 1) & (messageNumberCode[i-1] >> (3 - 1));
                         lsbG = ((1 << 1) - 1) & (messageNumberCode[i-1] >> (2 - 1));
                         lsbB = ((1 << 1) - 1) & (messageNumberCode[i-1] >> (1 - 1));
               
                        //update pixels lsb values
                        ppm->pixels[a][j].red |= lsbR;
                        ppm->pixels[a][j].green |= lsbG;
                        ppm->pixels[a][j].blue |= lsbB;
 
 
                        printf("\nWriting RHS of %d at pos %d. The RGB of this pixel is %d%d%d. Counter is %d. pos is %d", messageNumberCode[i-1], randomPixelIndex[i], lsbR, lsbG, lsbB, counter, pos);
                        counter = 0;
                    }
                }
                pos++;
            }
        }


In your else block, try changing your messageNumberCode index from i-1 to i.

'i' only gets incremented after you've supposedly looped through the whole image and written the entire character.
 

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/1038631-c-programming-help/#findComment-12342600
Share on other sites

Link to post
Share on other sites

7 hours ago, straight_stewie said:

In your else block, try changing your messageNumberCode index from i-1 to i.

When I do this, it only writes 1 side of each character:

 

t => 20 | z => 26 | w => 23 | q => 17 | n => 14 |
Number of Characters: 4rPixel: 13 | rPixel: 0 | rPixel: 6 | rPixel: 3 | rPixel: 12 | rPixel: 2 | rPixel: 14 | rPixel: 15 |
Writing LHS of 20 at pos 13. The RGB of this pixel is 010. Counter is 0. pos is 13
Writing RHS of 26 at pos 0. The RGB of this pixel is 010. Counter is 1. pos is 0
Writing LHS of 23 at pos 6. The RGB of this pixel is 010. Counter is 0. pos is 6
Writing RHS of 17 at pos 3. The RGB of this pixel is 001. Counter is 1. pos is 3
Writing LHS of 14 at pos 12. The RGB of this pixel is 100. Counter is 0. pos is 12
Writing LHS of 0 at pos 14. The RGB of this pixel is 000. Counter is 0. pos is 14

 

Link to comment
https://linustechtips.com/topic/1038631-c-programming-help/#findComment-12343323
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×