Here you can find the source of sample9Points(BufferedImage src, int x, int y)
Parameter | Description |
---|---|
src | a parameter |
x | a parameter |
y | a parameter |
public static Color sample9Points(BufferedImage src, int x, int y)
//package com.java2s; /*//from ww w . j a va 2 s.c o m * Copyright 2015 Laszlo Balazs-Csiki * * This file is part of Pixelitor. Pixelitor is free software: you * can redistribute it and/or modify it under the terms of the GNU * General Public License, version 3 as published by the Free * Software Foundation. * * Pixelitor is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Pixelitor. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Color; import java.awt.image.BufferedImage; public class Main { /** * Samples 9 pixels at and around the given pixel coordinates * * @param src * @param x * @param y * @return the average color */ public static Color sample9Points(BufferedImage src, int x, int y) { int averageRed = 0; int averageGreen = 0; int averageBlue = 0; int width = src.getWidth(); int height = src.getHeight(); for (int i = x - 1; i < x + 2; i++) { for (int j = y - 1; j < y + 2; j++) { int limitedX = limitSamplingIndex(i, width - 1); int limitedY = limitSamplingIndex(j, height - 1); int rgb = src.getRGB(limitedX, limitedY); // int a = (rgb >>> 24) & 0xFF; int r = (rgb >>> 16) & 0xFF; int g = (rgb >>> 8) & 0xFF; int b = (rgb) & 0xFF; averageRed += r; averageGreen += g; averageBlue += b; } } return new Color(averageRed / 9, averageGreen / 9, averageBlue / 9); } private static int limitSamplingIndex(int x, int max) { int r = x; if (r < 0) { r = 0; } if (r > max) { r = max; } return r; } }