JAVA - 复制图像,修改图像的特定像素并将其另存为临时.png文件

时间:2016-01-15 06:36:24

标签: java graphics pixels

我有一个64 x 64像素的png-texturemap,我需要复制它的一部分并将其粘贴到原始位置下方几个像素,因为我有一个稍微修改过的3dmodel,它的纹理贴图并不完全适合原来的。但是代码我写了错误,我无法确定问题所在的位置。

这是我到目前为止所做的:

public ResourceLocation getTemporaryTexturefile(AbstractClientPlayer player) {
        try {
            BufferedImage texture = ImageIO.read(Minecraft.getMinecraft().getResourceManager()
                    .getResource(player.getLocationSkin()).getInputStream());
            if (texture != null) {
                byte[] pixels = ((DataBufferByte) texture.getRaster().getDataBuffer()).getData();
                // copying the armtexture (sides)
                ArrayList<Byte> armtexture_bottom = new ArrayList<Byte>();
                ArrayList<Byte> armtexture_side = new ArrayList<Byte>();

                for (int pixelY = 0; pixelY < 4; pixelY++) {
                    // x locatet at 16 until 19 and y 48 to 52
                    for (int pixelX = 0; pixelX < 4; pixelX++) {
                        int posPixel = (16 * 64 + (pixelY * 64)) + 48 + pixelX;
                        armtexture_bottom.add(pixels[posPixel]);
                    }

                }

                for (int pixelY = 0; pixelY < 11; pixelY++) {
                    // 20 - 31 (x) & 40 - 56 (x)
                    for (int pixelX = 0; pixelX < 16; pixelX++) {
                        int posPixel = (pixelY * 64 + (40 + pixelX));
                        armtexture_side.add(pixels[posPixel]);
                    }

                }
                int counterY = 0;
                int counterX = 0;
                // pasting the two bytearrays onto the new image.
                for (int pixelY = 0; pixelY < 64; pixelY++) {
                    counterY++;
                    if (pixelY >= 24 && pixelY < 45) {
                        for (int pixelX = 0; pixelX < 64; pixelX++) {
                            if (pixelX > 39 && pixelY > 31) {
                                counterX++;
                                if (pixelY > 43) {
                                    int posinbytearray = (pixelY * 64 + pixelX) % 128;
                                    int rowYInByteArray = counterY - 43;
                                    pixels[pixelY * 64 + pixelX] = armtexture_side
                                            .get(posinbytearray - (16 * rowYInByteArray));
                                }
                            }
                            // pasting the top&bottom arm texture toimage array
                            if (pixelX > 47 && pixelX <= 51) {
                                int posinbytearray = ((pixelY * 64 + pixelX) % 16);
                                int rowYInByteArray = counterY - 23;
                                System.out.println(rowYInByteArray);
                                if((4 * rowYInByteArray + pixelX % 4)<=15){
                                pixels[pixelY * 64 + pixelX] = armtexture_bottom.get((4 * rowYInByteArray + pixelX % 4));
                                }else{
                                    System.out.println("Bigger than 15!!!!!");
                                }
                            }
                        }
                    }
                }
                InputStream newTextureBytes = new ByteArrayInputStream(pixels);
                BufferedImage newTexture = ImageIO.read(newTextureBytes);
                if (!new File(Minecraft.getMinecraft().mcDataDir.getPath() + "RLM/playerskin.png").exists()) {
                    System.out.println(Minecraft.getMinecraft().mcDataDir.getPath().toString());
                    ImageIO.write(newTexture, "png",new File(Minecraft.getMinecraft().mcDataDir.getPath() + "/RLM/playerskin.png"));
                }
                return new ResourceLocation("reallifemod:RLM/playerskin.png");
            }
        } catch (

        IOException e)

        {
            e.printStackTrace();
        }

        return null;

    }

}

但我的问题是,这个Codepart错误和4 * rowYInByteArray + pixelX%4大于15 - 复制部分的字节数是16 x 16像素的大小:

    pixels[pixelY * 64 + pixelX] = armtexture_bottom.get((4 * rowYInByteArray + pixelX % 4));
}else{
    System.out.println("Bigger than 15!!!!!");

另一个错误 - 此行错误与image == null异常:

ImageIO.write(newTexture, "png",new File(Minecraft.getMinecraft().mcDataDir.getPath() + "/RLM/playerskin.png"));

0 个答案:

没有答案