Jump to content

[1.12.2] How can I ignore transparent pixels in bufferedimage?


jbredwards

Recommended Posts

I've tried looking everywhere online with no luck, so I've come here. Since nothing I've tried works, I'm think that Minecraft .png images have an unusual way of reading transparency? I'm trying to find the average color in a texture (ignoring transparent pixels). It works for the most part, except it also ignores black/white pixels (pixels with the same rgb values, example r=222, g=222, b=222). The code I am using is in the spoiler. Any help would be appreciated!

Spoiler

//textureName is the file location of the texture (in the form of a String), which works fine.

try {
                        BufferedImage image = ImageIO.read(Minecraft.getMinecraft().getResourceManager().getResource(new ResourceLocation(textureName)).getInputStream());
                        int width = image.getWidth();
                        int height = image.getHeight();
                        float pixels = 0;
                        float r = 0;
                        float g = 0;
                        float b = 0;
                        for (int x = 0; x < width; x++) {
                            for (int y = 0; y < height; y++) {
                                Color color = new Color(image.getRGB(x, y));
                                if(color.getRGB() != -1) {
                                    pixels++;
                                    r += color.getRed();
                                    g += color.getGreen();
                                    b += color.getBlue();                       
                                }
                            }
                        }
                        if(pixels == width * height) {
                            float avgR = r / (pixels * 255.0F);
                            float avgG = g / (pixels * 255.0F);
                            float avgB = b / (pixels * 255.0F);
                            GlStateManager.color(avgR, avgG, avgB, 1.0F);
                            image.getGraphics().dispose();
                        }
                    } 
                    catch (IOException e) {
                        //e.printStackTrace();
                    }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.