Java examples for 2D Graphics:BufferedImage Scale
Scales image by mixing pixels to the scaled image.
import java.io.InputStream; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.game.Sprite; public class Main{ /**//from w w w . j a v a 2 s .c o m * Scales image by mixing pixels to the scaled image. Works best with down scaling. * @return Returns scaled image */ private static Image pixelMixing(Image original, int newWidth, int newHeight) { int[] rawInput = new int[original.getHeight() * original.getWidth()]; original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight()); int[] rawOutput = new int[newWidth * newHeight]; int oWidth = original.getWidth(); int[] oX16 = new int[newWidth + 1]; for (int newX = 0; newX <= newWidth; newX++) { oX16[newX] = ((newX * oWidth) << 4) / newWidth; } int[] oXStartWidth = new int[newWidth]; int[] oXEndWidth = new int[newWidth]; for (int newX = 0; newX < newWidth; newX++) { oXStartWidth[newX] = 16 - (oX16[newX] % 16); oXEndWidth[newX] = oX16[newX + 1] % 16; } int oHeight = original.getHeight(); int[] oY16 = new int[newHeight + 1]; for (int newY = 0; newY <= newHeight; newY++) { oY16[newY] = ((newY * oHeight) << 4) / newHeight; } int oX16Start, oX16End, oY16Start, oY16End; int oYStartHeight, oYEndHeight; int oXStart, oXEnd, oYStart, oYEnd; int outArea, outColorArea, outAlpha, outRed, outGreen, outBlue; int areaHeight, areaWidth, area; int argb, a, r, g, b; for (int newY = 0; newY < newHeight; newY++) { oY16Start = oY16[newY]; oY16End = oY16[newY + 1]; oYStart = oY16Start >>> 4; oYEnd = oY16End >>> 4; oYStartHeight = 16 - (oY16Start % 16); oYEndHeight = oY16End % 16; for (int newX = 0; newX < newWidth; newX++) { oX16Start = oX16[newX]; oX16End = oX16[newX + 1]; oXStart = oX16Start >>> 4; oXEnd = oX16End >>> 4; outArea = 0; outColorArea = 0; outAlpha = 0; outRed = 0; outGreen = 0; outBlue = 0; for (int j = oYStart; j <= oYEnd; j++) { areaHeight = 16; if (oYStart == oYEnd) { areaHeight = oY16End - oY16Start; } else if (j == oYStart) { areaHeight = oYStartHeight; } else if (j == oYEnd) { areaHeight = oYEndHeight; } if (areaHeight == 0) { continue; } for (int i = oXStart; i <= oXEnd; i++) { areaWidth = 16; if (oXStart == oXEnd) { areaWidth = oX16End - oX16Start; } else if (i == oXStart) { areaWidth = oXStartWidth[newX]; } else if (i == oXEnd) { areaWidth = oXEndWidth[newX]; } if (areaWidth == 0) { continue; } area = areaWidth * areaHeight; outArea += area; argb = rawInput[i + j * original.getWidth()]; a = (argb >>> 24); if (a == 0) { continue; } area = a * area; outColorArea += area; r = (argb & 0x00ff0000) >>> 16; g = (argb & 0x0000ff00) >>> 8; b = argb & 0x000000ff; outRed += area * r; outGreen += area * g; outBlue += area * b; } } if (outColorArea > 0) { outAlpha = outColorArea / outArea; outRed = outRed / outColorArea; outGreen = outGreen / outColorArea; outBlue = outBlue / outColorArea; } rawOutput[newX + newY * newWidth] = (outAlpha << 24) | (outRed << 16) | (outGreen << 8) | outBlue; } } return Image.createRGBImage(rawOutput, newWidth, newHeight, true); } }