title: Invert pixels - zxing
body: <p>I'm using a zxing library in my iOS project. It's a library for reading and creating QR Codes.</p>
  
  <p>From my researching and browsing around the web the process of decoding the image is made of this steps:</p>
  
  <ul>
  <li>takes an image from source, </li>
  <li>converts all the pixels to 255 grayscale</li>
  <li>decodes the data</li>
  </ul>
  
  <p>One thing which is not supported by this specific library is reading/decoding (and I'm pretty sure that this is missing in creating also) of inverted QRCodes. </p>
  
  <p>Inverted QR Codes are basicly the same as normal codes -> but with inverted colors (white is black and black is white). But because the QRCodes standard doesn't describe the inverted QRCodes implementation and on zxing project website there is a few requests and issues, I must implement this by myself.</p>
  
  <p>The method below is a good place to insert some logic to invert the pixels (<code>unsigned char*</code>), but because of my non-experience with C++ the result is this writing.</p>
  
  <p>The <code>grayData_</code> is a <code>unsigned char*</code> data type. And inside this variable there are grayScaled pixels from source. </p>
  
  <p>What I want to do is invert these pixels.</p>
  
  <p>If I'm correct this is done by <code>unsigned char cz = (255 - val);</code>?</p>
  
  <pre><code>unsigned char* GreyscaleLuminanceSource::getMatrix() {
      int size = width_ * height_;
      unsigned char* result = new unsigned char[size];
      if (left_ == 0 &amp;&amp; top_ == 0 &amp;&amp; dataWidth_ == width_ &amp;&amp; dataHeight_ == height_) {
          memcpy(result, greyData_, size);
      } else {
          for (int row = 0; row &lt; height_; row++) {
              memcpy(result + row * width_, greyData_ + (top_ + row) * dataWidth_ + left_, width_);
          }
      }
  
      //return result;
      //from this point down is my implementation
  
      printf(" %c", result[200]);
      printf(" %c", result[600]);
      printf(" %c", result[6000]);
      printf(" %c", result[7000]);
  
      for (int i = 0; i &lt; size; i++)
      {
          int val = static_cast&lt;int&gt;(result[i]); 
          unsigned char cz = (255 -  val);
          result[i] = cz;
      }
      printf("******\n");
      printf(" %c", result[200]); //prints a " " char to console/terminal
      printf(" %c", result[600]); //prints a " " char to console/terminal
      printf(" %c", result[6000]); //prints a " " char to console/terminal
      printf(" %c", result[7000]); //prints a " " char to console/terminal
  
      return result;
  }
  </code></pre>
  
  <p>Is this the right way to invert the pixels? And I'm not so happy with changing the data in the <code>result</code> variable.</p>