Java Code Examples for java.awt.image.ColorModel#getGreen()
The following examples show how to use
java.awt.image.ColorModel#getGreen() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Picture.java From Face-Recognition with GNU General Public License v3.0 | 6 votes |
public double[] getImagePixels() { int w = img.getWidth(this); int h = img.getHeight(this); int[] pixels = new int[w * h]; PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return new double[0]; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return new double[0]; } double[] ret =new double[w*h]; ColorModel cm = pg.getColorModel(); for (int i=0; i<ret.length; i++) { ret[i] = cm.getBlue(pixels[i]) + cm.getGreen(pixels[i]) + cm.getRed(pixels[i]); ret[i] /= 3.0; } return ret; }
Example 2
Source File: Picture.java From Face-Recognition with GNU General Public License v3.0 | 6 votes |
public double[] getImageColourPixels() { int w = img.getWidth(this); int h = img.getHeight(this); int[] pixels = new int[w * h]; PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return new double[0]; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return new double[0]; } double[] ret =new double[w*h]; ColorModel cm = pg.getColorModel(); for (int i=0; i<ret.length; i++) { Color c=new Color(cm.getRed(pixels[i]),cm.getGreen(pixels[i]),cm.getBlue(pixels[i])); ret[i]=c.getRGB(); } return ret; }
Example 3
Source File: ImageFilter.java From JewelCrawler with GNU General Public License v3.0 | 5 votes |
/** 图像二值化 */ public BufferedImage changeGrey() { PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih, pixels, 0, iw); try { pg.grabPixels(); } catch (InterruptedException e) { e.printStackTrace(); } // 设定二值化的域值,默认值为100 int grey = 100; // 对图像进行二值化处理,Alpha值保持不变 ColorModel cm = ColorModel.getRGBdefault(); for (int i = 0; i < iw * ih; i++) { int red, green, blue; int alpha = cm.getAlpha(pixels[i]); if (cm.getRed(pixels[i]) > grey) { red = 255; } else { red = 0; } if (cm.getGreen(pixels[i]) > grey) { green = 255; } else { green = 0; } if (cm.getBlue(pixels[i]) > grey) { blue = 255; } else { blue = 0; } pixels[i] = alpha << 24 | red << 16 | green << 8 | blue; } // 将数组中的象素产生一个图像 return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih, pixels, 0, iw)); }
Example 4
Source File: ImageFilter.java From JewelCrawler with GNU General Public License v3.0 | 5 votes |
/** 线性灰度变换 */ public BufferedImage lineGrey() { PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih, pixels, 0, iw); try { pg.grabPixels(); } catch (InterruptedException e) { e.printStackTrace(); } // 对图像进行进行线性拉伸,Alpha值保持不变 ColorModel cm = ColorModel.getRGBdefault(); for (int i = 0; i < iw * ih; i++) { int alpha = cm.getAlpha(pixels[i]); int red = cm.getRed(pixels[i]); int green = cm.getGreen(pixels[i]); int blue = cm.getBlue(pixels[i]); // 增加了图像的亮度 red = (int) (1.1 * red + 30); green = (int) (1.1 * green + 30); blue = (int) (1.1 * blue + 30); if (red >= 255) { red = 255; } if (green >= 255) { green = 255; } if (blue >= 255) { blue = 255; } pixels[i] = alpha << 24 | red << 16 | green << 8 | blue; } // 将数组中的象素产生一个图像 return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih, pixels, 0, iw)); }
Example 5
Source File: PNGEncoder.java From tn5250j with GNU General Public License v2.0 | 5 votes |
/** * @param outarray * @param pixelarray * @param cmodel * @param width * @param height * @return * @throws EncoderException */ public int compress(byte[] outarray, int[] pixelarray, ColorModel cmodel, int width, int height) throws EncoderException { byte[] inarray = new byte[(pixelarray.length * 3) + height]; for (int i = 0; i < height; i++) { inarray[i * ((width * 3) + 1)] = byteFromInt(0); for (int j = 0; j < (width * 3); j+= 3) { inarray[(i * ((width * 3) + 1)) + j + 1] = (byte) cmodel.getRed(pixelarray[(i * width) + (int)Math.floor(j / 3)]); inarray[(i * ((width * 3) + 1)) + j + 2] = (byte) cmodel.getGreen(pixelarray[(i * width) + (int)Math.floor(j / 3)]); inarray[(i * ((width * 3) + 1)) + j + 3] = (byte) cmodel.getBlue(pixelarray[(i * width) + (int)Math.floor(j / 3)]); } } return compressInternal(outarray, inarray); }
Example 6
Source File: WebP.java From webp-imageio with Apache License 2.0 | 5 votes |
private static byte[] extractGenericRGB( RenderedImage aRi, int aWidth, int aHeight, ColorModel aColorModel ) { Object dataElements = null; byte[] rgbData = new byte[ aWidth * aHeight * 3 ]; for ( int b = 0, y = 0; y < aHeight; y++ ) { for ( int x = 0; x < aWidth; x++, b += 3 ) { dataElements = aRi.getData().getDataElements( x, y, dataElements ); rgbData[ b ] = ( byte ) aColorModel.getRed( dataElements ); rgbData[ b + 1 ] = ( byte ) aColorModel.getGreen( dataElements ); rgbData[ b + 2 ] = ( byte ) aColorModel.getBlue( dataElements ); } } return rgbData; }
Example 7
Source File: WebP.java From webp-imageio with Apache License 2.0 | 5 votes |
private static byte[] extractGenericRGBA( RenderedImage aRi, int aWidth, int aHeight, ColorModel aColorModel ) { Object dataElements = null; byte[] rgbData = new byte[ aWidth * aHeight * 4 ]; for ( int b = 0, y = 0; y < aHeight; y++ ) { for ( int x = 0; x < aWidth; x++, b += 4 ) { dataElements = aRi.getData().getDataElements( x, y, dataElements ); rgbData[ b ] = ( byte ) aColorModel.getRed( dataElements ); rgbData[ b + 1 ] = ( byte ) aColorModel.getGreen( dataElements ); rgbData[ b + 2 ] = ( byte ) aColorModel.getBlue( dataElements ); rgbData[ b + 3 ] = ( byte ) aColorModel.getAlpha( dataElements ); } } return rgbData; }
Example 8
Source File: SoftMask.java From gcs with Mozilla Public License 2.0 | 4 votes |
@Override public Raster getRaster(int x1, int y1, int w, int h) { Raster raster = context.getRaster(x1, y1, w, h); ColorModel rasterCM = context.getColorModel(); float[] input = null; Float[] map = null; if (transferFunction != null) { map = new Float[256]; input = new float[1]; } // buffer WritableRaster output = getColorModel().createCompatibleWritableRaster(w, h); // the soft mask has its own bbox x1 = x1 - (int)bboxDevice.getX(); y1 = y1 - (int)bboxDevice.getY(); int[] gray = new int[4]; Object pixelInput = null; int[] pixelOutput = new int[4]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { pixelInput = raster.getDataElements(x, y, pixelInput); pixelOutput[0] = rasterCM.getRed(pixelInput); pixelOutput[1] = rasterCM.getGreen(pixelInput); pixelOutput[2] = rasterCM.getBlue(pixelInput); pixelOutput[3] = rasterCM.getAlpha(pixelInput); // get the alpha value from the gray mask, if within mask bounds gray[0] = 0; if (x1 + x >= 0 && y1 + y >= 0 && x1 + x < mask.getWidth() && y1 + y < mask.getHeight()) { mask.getRaster().getPixel(x1 + x, y1 + y, gray); int g = gray[0]; if (transferFunction != null) { // apply transfer function try { if (map[g] != null) { // was calculated before pixelOutput[3] = Math.round(pixelOutput[3] * map[g]); } else { // calculate and store in map input[0] = g / 255f; float f = transferFunction.eval(input)[0]; map[g] = f; pixelOutput[3] = Math.round(pixelOutput[3] * f); } } catch (IOException ex) { // ignore exception, treat as outside pixelOutput[3] = Math.round(pixelOutput[3] * (bc / 255f)); } } else { pixelOutput[3] = Math.round(pixelOutput[3] * (g / 255f)); } } else { pixelOutput[3] = Math.round(pixelOutput[3] * (bc / 255f)); } output.setPixel(x, y, pixelOutput); } } return output; }
Example 9
Source File: ImageFilter.java From JewelCrawler with GNU General Public License v3.0 | 4 votes |
/** 提升清晰度,进行锐化 */ public BufferedImage sharp() { PixelGrabber pg = new PixelGrabber(image.getSource(), 0, 0, iw, ih, pixels, 0, iw); try { pg.grabPixels(); } catch (InterruptedException e) { e.printStackTrace(); } // 象素的中间变量 int tempPixels[] = new int[iw * ih]; for (int i = 0; i < iw * ih; i++) { tempPixels[i] = pixels[i]; } // 对图像进行尖锐化处理,Alpha值保持不变 ColorModel cm = ColorModel.getRGBdefault(); for (int i = 1; i < ih - 1; i++) { for (int j = 1; j < iw - 1; j++) { int alpha = cm.getAlpha(pixels[i * iw + j]); // 对图像进行尖锐化 int red6 = cm.getRed(pixels[i * iw + j + 1]); int red5 = cm.getRed(pixels[i * iw + j]); int red8 = cm.getRed(pixels[(i + 1) * iw + j]); int sharpRed = Math.abs(red6 - red5) + Math.abs(red8 - red5); int green5 = cm.getGreen(pixels[i * iw + j]); int green6 = cm.getGreen(pixels[i * iw + j + 1]); int green8 = cm.getGreen(pixels[(i + 1) * iw + j]); int sharpGreen = Math.abs(green6 - green5) + Math.abs(green8 - green5); int blue5 = cm.getBlue(pixels[i * iw + j]); int blue6 = cm.getBlue(pixels[i * iw + j + 1]); int blue8 = cm.getBlue(pixels[(i + 1) * iw + j]); int sharpBlue = Math.abs(blue6 - blue5) + Math.abs(blue8 - blue5); if (sharpRed > 255) { sharpRed = 255; } if (sharpGreen > 255) { sharpGreen = 255; } if (sharpBlue > 255) { sharpBlue = 255; } tempPixels[i * iw + j] = alpha << 24 | sharpRed << 16 | sharpGreen << 8 | sharpBlue; } } // 将数组中的象素产生一个图像 return ImageIOHelper.imageProducerToBufferedImage(new MemoryImageSource(iw, ih, tempPixels, 0, iw)); }
Example 10
Source File: SoftMask.java From sambox with Apache License 2.0 | 4 votes |
@Override public Raster getRaster(int x1, int y1, int w, int h) { Raster raster = context.getRaster(x1, y1, w, h); ColorModel rasterCM = context.getColorModel(); float[] input = null; Float[] map = null; if (transferFunction != null) { map = new Float[256]; input = new float[1]; } // buffer WritableRaster output = getColorModel().createCompatibleWritableRaster(w, h); // the soft mask has its own bbox x1 = x1 - (int) bboxDevice.getX(); y1 = y1 - (int) bboxDevice.getY(); int[] gray = new int[4]; Object pixelInput = null; int[] pixelOutput = new int[4]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { pixelInput = raster.getDataElements(x, y, pixelInput); pixelOutput[0] = rasterCM.getRed(pixelInput); pixelOutput[1] = rasterCM.getGreen(pixelInput); pixelOutput[2] = rasterCM.getBlue(pixelInput); pixelOutput[3] = rasterCM.getAlpha(pixelInput); // get the alpha value from the gray mask, if within mask bounds gray[0] = 0; if (x1 + x >= 0 && y1 + y >= 0 && x1 + x < mask.getWidth() && y1 + y < mask.getHeight()) { mask.getRaster().getPixel(x1 + x, y1 + y, gray); int g = gray[0]; if (transferFunction != null) { // apply transfer function try { if (map[g] != null) { // was calculated before pixelOutput[3] = Math.round(pixelOutput[3] * map[g]); } else { // calculate and store in map input[0] = g / 255f; float f = transferFunction.eval(input)[0]; map[g] = f; pixelOutput[3] = Math.round(pixelOutput[3] * f); } } catch (IOException ex) { // ignore exception, treat as outside pixelOutput[3] = Math.round(pixelOutput[3] * (bc / 255f)); } } else { pixelOutput[3] = Math.round(pixelOutput[3] * (g / 255f)); } } else { pixelOutput[3] = Math.round(pixelOutput[3] * (bc / 255f)); } output.setPixel(x, y, pixelOutput); } } return output; }