用C嵌入到PPM p6文件中,如何屏蔽位?

时间:2016-05-02 22:05:09

标签: c char embed unsigned ppm

我试图在PPM文件中隐藏消息,(p6)。我非常喜欢用红色像素对我的消息进行异或。这很有效,但对图像做了非常明显的改变。我知道更好的方法是使用位掩码,或者根据需要将每个LSB位更改为1或0。我在C中遇到这个问题。

我的数据结构......

typedef struct {
    unsigned char red,green,blue;
} PPMPixel;

typedef struct {
    int x, y;
    PPMPixel *data;
} PPMImage;

和我的嵌入功能...

unsigned char * embBits(PPMImage *img, int messageLen, unsigned char*msgOne, unsigned char *encMsg)
{
    int count = 0;
    unsigned char eMsg[messageLen]; 
    unsigned char bit;


    for(int j = 0; j < messageLen; j++)
    {
        //bit = msgOne[j];

        img->data[count].red = msgOne[j] ^ img->data[count].red;
    //  img->data[count].green = msgOne[j+1] ^ img->data[count].green; 
    //  img->data[count].blue = msgOne[j+2] ^ img->data[count].blue;
        eMsg[j] = img->data[count].red;
        count++;
    /*  
    // was thinking to look at each bit in RED / GREEN / BLUE
    // 1. start at 100000 and go down to 000001
    // 2. leave values that are the same / flip values that are different

        for(int k=7; 0 < k; k--)
        {               
            // bit & ( 1 << k);
        }
    */
    }

    return encMsg = eMsg;
}

我知道如果他们不匹配我需要翻转LSB,但是我很难想到如何在C中做到这一点。起初我在想第一个角色流 - 将其转换为十六进制,将其转换为二进制,然后取第一位并将其屏蔽为红色像素,第二位使用绿色像素,依此类推。我知道只需一个简单的移位和按位&amp;就可以找到一个更简单的解决方案,但我还没弄清楚。谢谢你的帮助。

UPDATED ::::我还没有能够测试这个,但我认为只需看一下即可。我只需要更新我的函数输出,以捕获我正在改变的新值。

unsigned char * embBits(PPMImage *img, int messageLen, unsigned char*msgOne, unsigned char *encMsg)
{
    int count = 0;
    unsigned char eMsg[messageLen]; 
    unsigned char byte;
    unsigned char mask;
    unsigned char flipOne = 0x01;
    unsigned char flipZero = 0x00;

    for(int j = 0; j < messageLen; j++)
    {
        byte = msgOne[j];
        mask = 0x80;    

        for(int k=7; 0 < k; k--)
        {



byte = byte & mask;

            if(byte == 0)
            {
                //red
                if (mask == 0x80 || mask == 0x10 || mask == 0x02)
                {
                    //check LSB and FLIP 
                    img->data[count].red = img->data[count].red & flipZero;     
                }

                //green
                if (mask == 0x40 || mask == 0x08 || mask == 0x01)
                {
                    //check LSB and FLIP 
                    img->data[count].green = img->data[count].green & flipZero;     
                }

                //blue
                if (mask == 0x20 || mask == 0x03)
                {
                    //check

 LSB and FLIP 
                    img->data[count].blue = img->data[count].blue & flipZero;       
                }

            }   
            else
            {
                //flip bit
                //red
                if (mask == 0x80 || mask == 0x10 || mask == 0x02)
                {
                    //check LSB and FLIP 
                    img->data[count].red = img->data[count].red & flipOne;      
                }

                //green
                if (mask == 0x40 || mask == 0x08 || mask == 0x01)
                {


//check LSB and FLIP 
            img->data[count].green = img->data[count].green & flipOne;      
        }

        //blue
        if (mask == 0x20 || mask == 0x03)
        {
            //check LSB and FLIP 
            img->data[count].blue = img->data[count].blue & flipOne;        
        }



        }   

            mask = mask >> 1;
            count++;
        }

    }

    return encMsg = eMsg;
}

0 个答案:

没有答案