0

I saw this example here where that program embeds a text message into the LSB of the Alpha byte of a pixel but I don't trully understand everything .How can I embed the information I want only in the LSB of the Red byte or the Blue byte or the Green one from a pixel ?

Here are the functions where I think I should work on(so you don't have to go to the site):

private void embedInteger(BufferedImage img,int n,int start,int storageBit){
    int maxX=img.getWidth(),maxY=img.getHeight(),startX=start/maxY,startY=start-startX*maxY,count=0;
    for(int i=startX;i<maxX && count<32;i++){
        for(int j=startY;j<maxY && count<32;j++){
            int rgb=img.getRGB(i, j),bit=getBitValue(n,count);
            rgb=setBitValue(rgb,storageBit,bit);
            img.setRGB(i,j,rgb);
            count++;
        }
    }
}


private void embedByte(BufferedImage img,byte b,int start,int storageBit){
    int maxX=img.getWidth(),maxY=img.getHeight(),startX=start/maxY,startY=start-startX*maxY,count=0;
    for(int i=startX;i<maxX && count<8;i++){
        for(int j=startY;j<maxY && count<8;j++){
            int rgb=img.getRGB(i,j),bit=getBitValue(b,count);
            rgb=setBitValue(rgb,storageBit,bit);
            img.setRGB(i,j,rgb);
            count++;
        }
    }
}

private int getBitValue(int n,int location){
    int v= n & (int) Math.round(Math.pow(2, location));
    return v==0?0:1;
}

private int setBitValue(int n,int location,int bit){
    int toggle = (int)Math.pow(2, location),bv=getBitValue(n,location);
    if(bv==bit)
        return n;
    if(bv==0&&bit==1)
        n |= toggle;
    else if(bv==1 && bit==0)
        n^=toggle;
    return n;
}
w0wy
  • 21
  • 3
  • Do you understand figure 1 from the link you posted? Do you know which bits occupy the red pixel? Do you understand which bit is the lsb of the red pixel? Use that for `storage Bit` when you call `embedByte` and you're good to go. – Reti43 May 22 '15 at 20:46
  • Holy mother of .... Sorry for this kinda stupid question but now I understand and that was quite ... simple . Thank you for your answer ! – w0wy May 22 '15 at 21:05
  • Keep in mind you have to apply that change for `embedInteger`, `embedByte` and their respective extraction methods. Also, if you intend to use that code, be wary that it has a bug which is covered [here](http://stackoverflow.com/questions/21267677/failing-to-decode-as-same-length-as-input/21318847). – Reti43 May 22 '15 at 21:12

0 Answers0