Reputation: 887
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).
The thing is I never get the exact same effect...
I tried following methods:
1) java-image-scale by mortennobel lib
resampleOp.setFilter(ResampleFilters.getBoxFilter());
this performs great, but leaves some artifacts in image, that are not there when photoshop does it.
2) TwelveMonkeys library for image manipulation. (here is the github link) Did not work as well, PointFilter completely destroys the gradient inside, and the Box filter does same thing as mortennobel getBoxFilter.
3) AWT AffineTransform, this was the worst one, completely unrealistic resize.
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?)
Here is the before and after images Photoshop generates
And here is the image generated by get BoxFilter from mortennobel lib.
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.
Upvotes: 4
Views: 2818
Reputation: 887
Huge Thanks to Marco13 for pointing this out! Apparently mortennobel lib doesnot do Nearest Neighbour, instead AWT's Graphics2D can if used with RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR rendering hint. Here is the code snippet that worked for me and produced photoshop's exact image.
destinationBufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = destinationBufferedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2.drawImage(sourceBufferedImage, 0, 0, newWidth, newHeight, null);
g2.dispose();
Upvotes: 2