I’m writing a program for hiding data in PNG files. Due to the lossless compression of PNGs you can slightly modify the colors of a pixel to store data. The problem I have is reading a &[u8]
of data bit by bit (in this case two bits but I’d like something that works for multiple bit counts).
Context:
let (mut R, mut G, mut B) = image.get_pixel(x,y);
r = r & 0xFC; // 0b11111100 removes last two bits
r = r + data.read_bits(2); // read two bits
g = g & 0xFC;
g = g + data.read_bits(2);
b = b & 0xFC;
b = b + data.read_bits(2);
image.put_pixel(x,y,Pixel::RGB(r, g, b);
Since I also want to get the hidden data back I’d also be nice to get some advice on how to reverse this process.
I’ve tried having a counter that counts the amount of bits read by the reader, but this solution only works of the number of bits being read is a divisor of 8 since other values would cause the ‘window’ to overlap with ‘borders’ of bytes.
lynx_xdg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.