Solved Preserving color when shrinking a BufferedImage

Discussion in 'Plugin Development' started by Zombie_Striker, Jun 10, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Zombie_Striker

    Code:.
    Code:
        public static BufferedImage createResizedCopy(BufferedImage originalImage,
                int scaledHeight, boolean preserveAlpha) {
            int imageType = BufferedImage.TYPE_INT_RGB;
            int WIDTH = (int) (((double) originalImage.getWidth()) * (((double) scaledHeight) / originalImage
                    .getHeight()));
            int HEIGHT = scaledHeight;
            BufferedImage scaledBI = new BufferedImage(WIDTH, HEIGHT, imageType);
            Graphics2D g = scaledBI.createGraphics();
            if (preserveAlpha) {
                g.setComposite(AlphaComposite.Src);
            }
            g.drawImage(originalImage, 0, 0, (int) scaledBI.getWidth(),
                    scaledBI.getHeight(), null);
            g.dispose();
    
            return scaledBI;
        }

    Problem:
    . When resizing the image to something less than the original image size, the exact RGB color for each pixel gets shifted in some way (sometimes it gets lighter, sometimes darker). For example, base-minecraft's hay block becomes brown when converted to a 2x2 image.

    What I am trying to achieve is a way to preserve the exact color for each pixel, so that they hay block texture stays yellow.

    What you can do to recreate the problem:. Take any buffered image and reduce it to a 2x2 image.

    Possible problematic line(s):
    . unknown

    The FULL Error log:
    . n/a
     
  2. Offline

    CeramicTitan

Thread Status:
Not open for further replies.

Share This Page