title: Invert image pixels
body: <p>I'm currently trying to convert a piece of matlab code to java. The purpose of the code is to invert and normalize the image pixels of an image file. In java, the pixels are stored in a byte array. Below is the Matlab code of importance:</p>
  
  <pre><code>inp2=1024.-inp.-min; %inp is the input array (double precision). min is the minimum value in that matrix.
  </code></pre>
  
  <p>The image is 16 bit, but is using only 10 bits for storage, so that's where the 1024 comes from (2^10). I know definitively that this code works in matlab. However, I'm personally not proficient in matlab, and my java translation isn't behaving the same way as its counterpart.</p>
  
  <p>Below is the method where I've tried inverting the image matrix:</p>
  
  <pre><code>//bitsStored is the bit depth. In this test, it is 10.
  //imageBytes is the pixel data in a byte array
  public static short[] invert(int bitsStored) {
      short min = min(imageBytes);//custom method. Gets the minimum value in the byte array. 
      short range = (short) (2 &lt;&lt; bitsStored);
      short[] holder = new short[imageBytes.length];
      for (int i = 0; i &lt; imageBytes.length; i++) {
          holder[i] = (short) (range - imageBytes[i] - min);
      }
      imageBytes = holder;
      return imageBytes;
  }
  </code></pre>
  
  <p>However, instead of inverting the color channels, the image loses some data and becomes much harsher looking (higher contrast, less blend, etc). What am I doing wrong here?</p>
  
  <p>Let me know if I can make anything clearer for you. Thank you.</p>
  
  <p><strong>UPDATE</strong>:
  Hi, I have another question regarding this code. Can the above code (fixed to short[] not byte[]) be used in reverse on the same file? As in, if I rerun through this code using an inverted version of the original image, should I get the original input/image from the start of the program? The only problem with it I think is that the min value changes between runs.</p>