title: Java Image Re-sizing - Nearest Neighbor
body: <p>I am trying to re-size an image with Java, and I need to get the exact effect that is happening when you re-size with Photoshop Nearest Neighbor (Preserve hard edges).</p>
  
  <p>The thing is I never get the exact same effect...</p>
  
  <p>I tried following methods:</p>
  
  <p>1) java-image-scale by <a href="https://code.google.com/p/java-image-scaling/" rel="nofollow noreferrer">mortennobel lib</a></p>
  
  <pre><code>resampleOp.setFilter(ResampleFilters.getBoxFilter());
  </code></pre>
  
  <p>this performs great, but leaves some artifacts in image, that are not there when photoshop does it.</p>
  
  <p>2) TwelveMonkeys library for image manipulation. (<a href="https://github.com/haraldk/TwelveMonkeys" rel="nofollow noreferrer">here is the github link</a>)
  Did not work as well, PointFilter completely destroys the gradient inside, and the Box filter does same thing as mortennobel getBoxFilter.</p>
  
  <p>3) AWT AffineTransform, this was the worst one, completely unrealistic resize.</p>
  
  <p>Now I am confused, does photoshop's nearest neighbor re-size differently then what the name means, or all the other libs do it wrong (in second case, what is the lib that will do it right?)</p>
  
  <p>Here is the before and after images Photoshop generates</p>
  
  <p><img src="https://i.stack.imgur.com/qrLHs.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/FWmOX.png" alt="enter image description here"></p>
  
  <p>And here is the image generated by get BoxFilter from mortennobel lib.</p>
  
  <p><img src="https://i.stack.imgur.com/gHOI8.png" alt="enter image description here"></p>
  
  <p>I scaled images a bit up so you can see the details, in reality they are smaller.
  Any help is really appreciated :) I am really stuck on this.</p>