title: Steganography: how to change the LSB of a pixel array
body: <p>I'm using the lodePNG library to encode a png image and change the LSB of the pixels with an imported txt file. I've compiled the program but I'm not sure if the PNG file is actually being encoded according to my bitwise operation. </p>
  
  <p>The lodePNG library decodes/encodes from a PNG image and stores the pixels in the vector "image", 4 bytes per pixel, ordered RGBARGBA...,</p>
  
  <pre><code>void decodeOneStep(const char* filename)
  {
  
        unsigned width, height;
  
        //decode
        unsigned error = lodepng::decode(image, width, height, filename);
  
        //if there's an error, display it
        if (error) std::cout &lt;&lt; "decoder error " &lt;&lt; error &lt;&lt; ": " &lt;&lt; 
            lodepng_error_text(error) &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The program takes a command line argument of the text file and the PNG file. I have not included error-checking for arguments yet. </p>
  
  <pre><code>int const MAX_SIZE = 100; 
  std::vector&lt;unsigned char&gt; image;
  
  int main(int argc, char *argv[])
  {
  const char* filename;
  char* textArray = new char[MAX_SIZE];
  
  std::ifstream textfile;
  textfile.open(argv[1]);
  
  int numCount = 0;
  while (!textfile.eof() &amp;&amp; numCount &lt; MAX_SIZE)
  {
      textfile.get(textArray[numCount]); //reading single character from file to array
      numCount++;
  }
  
  textfile.close();
  
      filename = argv[2];
      decodeOneStep(filename);
  
      unsigned width = 512, height = 512;
      image.resize(width * height * 4);
  
      int pixCount = 0; 
  
      for (int i = 0; i &lt; numCount - 1; i++) {
  
          std::cout &lt;&lt; textArray[i]; 
  
          for (int j = 0; j &lt; 8; j++) {
  
              std::cout &lt;&lt; ((textArray[i]) &amp; 1); //used to see actual bit value.
              image[pixCount++] |= ((textArray[i]) &amp; 1);
              (textArray[i]) &gt;&gt;= 1; 
          }                       
          std::cout &lt;&lt; std::endl; 
      } 
  
  encodeOneStep(filename, image, width, height);
  </code></pre>
  
  <p>In the for-loop, I am going through each pixel in the vector and replacing LSB with a bit from the char. Since a char is 8 bytes, the for-loop loops 8 times. This program should work for most PNG images and texts not exceeding size, but I'm not sure the bitwise operation is actually doing anything. Also, how would I be able to shift the bits so that we store the char bits from MSB to LSB? I feel like I'm understanding something wrong with how the pixel values (bits) are stored in the array. </p>
  
  <p>EDIT: Test i've run on the new bit-operation: </p>
  
  <pre><code>    for (int j = 7; j &gt;= 0; j--) {
  
          //These tests were written to see if the 4-bits of the pixels were actually being replaced.
          //The LSB of the pixel bits are replaced with the MSB of the text character. 
  
          std::cout &lt;&lt;"Initial pixel 4-bits: " &lt;&lt; std::bitset &lt;4&gt;(image[pixCount]) &lt;&lt; "  "; 
          std::cout &lt;&lt; "MSB of char: " &lt;&lt; ((textArray[i] &gt;&gt; j) &amp; 0x01) &lt;&lt; " ";
          std::cout &lt;&lt; "Pixel LSB replaced: " &lt;&lt; ((image[pixCount] &amp; mask) | ((textArray[i] &gt;&gt; j) &amp; 0x01)) &lt;&lt; " ";
  
          image[pixCount] = (image[pixCount] &amp; mask) | ((textArray[i] &gt;&gt; j) &amp; 0x01);
          pixCount++;
  
          std::cout &lt;&lt; std::endl;
  
      }
  </code></pre>
  
  <p>Test Result: </p>
  
  <pre><code>For char 'a' 
  Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
  Initial pixel 4-bits : 0001 MSB: 1 Pixel LSB replaced: 1
  Initial pixel 4-bits : 0001 MSB: 1 Pixel LSB replaced: 1
  Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
  Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
  Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
  Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
  Initial pixel 4-bits : 0001 MSB: 1 Pixel LSB replaced: 1
  </code></pre>