Java Code Examples for java.awt.image.ColorModel#getAlpha()

The following examples show how to use java.awt.image.ColorModel#getAlpha() . 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: WBMPPluginTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean compare(BufferedImage in, BufferedImage out) {
    int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j);
            }
        }
    }
    return true;
}
 
Example 2
Source File: BMPCompressionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean compare(final BufferedImage in, final BufferedImage out) {

            final int width = in.getWidth();
            int height = in.getHeight();
            if (out.getWidth() != width || out.getHeight() != height) {
                throw new RuntimeException("Dimensions changed!");
            }

            Raster oldras = in.getRaster();
            ColorModel oldcm = in.getColorModel();
            Raster newras = out.getRaster();
            ColorModel newcm = out.getColorModel();

            for (int j = 0; j < height; j++) {
                for (int i = 0; i < width; i++) {
                    Object oldpixel = oldras.getDataElements(i, j, null);
                    int oldrgb = oldcm.getRGB(oldpixel);
                    int oldalpha = oldcm.getAlpha(oldpixel);

                    Object newpixel = newras.getDataElements(i, j, null);
                    int newrgb = newcm.getRGB(newpixel);
                    int newalpha = newcm.getAlpha(newpixel);

                    if (newrgb != oldrgb ||
                        newalpha != oldalpha) {
                        // showDiff(in, out);
                        throw new RuntimeException("Pixels differ at " + i +
                                                   ", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));
                    }
                }
            }
            return true;
        }
 
Example 3
Source File: BMPPluginTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean compare(BufferedImage in, BufferedImage out) {
    int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j);
            }
        }
    }
    return true;
}
 
Example 4
Source File: BMPWriteParamTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean compare(final BufferedImage in,
                               final BufferedImage out)
{
    final int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                // showDiff(in, out);
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));
            }
        }
    }
    return true;
}
 
Example 5
Source File: ImageCompare.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void compare(BufferedImage oldimg,
                           BufferedImage newimg) {
    int width = oldimg.getWidth();
    int height = oldimg.getHeight();
    if (newimg.getWidth() != width || newimg.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = oldimg.getRaster();
    ColorModel oldcm = oldimg.getColorModel();
    Raster newras = newimg.getRaster();
    ColorModel newcm = newimg.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j);
            }
        }
    }
}
 
Example 6
Source File: ImageFilter.java    From JewelCrawler with GNU General Public License v3.0 5 votes vote down vote up
/** 图像二值化 */
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 7
Source File: ImageFilter.java    From JewelCrawler with GNU General Public License v3.0 5 votes vote down vote up
/** 线性灰度变换 */
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 8
Source File: WBMPPluginTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean compare(BufferedImage in, BufferedImage out) {
    int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j);
            }
        }
    }
    return true;
}
 
Example 9
Source File: BMPCompressionTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean compare(final BufferedImage in, final BufferedImage out) {

            final int width = in.getWidth();
            int height = in.getHeight();
            if (out.getWidth() != width || out.getHeight() != height) {
                throw new RuntimeException("Dimensions changed!");
            }

            Raster oldras = in.getRaster();
            ColorModel oldcm = in.getColorModel();
            Raster newras = out.getRaster();
            ColorModel newcm = out.getColorModel();

            for (int j = 0; j < height; j++) {
                for (int i = 0; i < width; i++) {
                    Object oldpixel = oldras.getDataElements(i, j, null);
                    int oldrgb = oldcm.getRGB(oldpixel);
                    int oldalpha = oldcm.getAlpha(oldpixel);

                    Object newpixel = newras.getDataElements(i, j, null);
                    int newrgb = newcm.getRGB(newpixel);
                    int newalpha = newcm.getAlpha(newpixel);

                    if (newrgb != oldrgb ||
                        newalpha != oldalpha) {
                        // showDiff(in, out);
                        throw new RuntimeException("Pixels differ at " + i +
                                                   ", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));
                    }
                }
            }
            return true;
        }
 
Example 10
Source File: BMPPluginTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean compare(BufferedImage in, BufferedImage out) {
    int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j);
            }
        }
    }
    return true;
}
 
Example 11
Source File: BMPWriteParamTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean compare(final BufferedImage in,
                               final BufferedImage out)
{
    final int width = in.getWidth();
    int height = in.getHeight();
    if (out.getWidth() != width || out.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = in.getRaster();
    ColorModel oldcm = in.getColorModel();
    Raster newras = out.getRaster();
    ColorModel newcm = out.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                // showDiff(in, out);
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));
            }
        }
    }
    return true;
}
 
Example 12
Source File: ImageCompare.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void compare(BufferedImage oldimg,
                           BufferedImage newimg) {
    int width = oldimg.getWidth();
    int height = oldimg.getHeight();
    if (newimg.getWidth() != width || newimg.getHeight() != height) {
        throw new RuntimeException("Dimensions changed!");
    }

    Raster oldras = oldimg.getRaster();
    ColorModel oldcm = oldimg.getColorModel();
    Raster newras = newimg.getRaster();
    ColorModel newcm = newimg.getColorModel();

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            Object oldpixel = oldras.getDataElements(i, j, null);
            int oldrgb = oldcm.getRGB(oldpixel);
            int oldalpha = oldcm.getAlpha(oldpixel);

            Object newpixel = newras.getDataElements(i, j, null);
            int newrgb = newcm.getRGB(newpixel);
            int newalpha = newcm.getAlpha(newpixel);

            if (newrgb != oldrgb ||
                newalpha != oldalpha) {
                throw new RuntimeException("Pixels differ at " + i +
                                           ", " + j);
            }
        }
    }
}
 
Example 13
Source File: WebP.java    From webp-imageio with Apache License 2.0 5 votes vote down vote up
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 14
Source File: SoftMask.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@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 15
Source File: ImageFilter.java    From JewelCrawler with GNU General Public License v3.0 4 votes vote down vote up
/** 提升清晰度,进行锐化 */
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 16
Source File: SoftMask.java    From sambox with Apache License 2.0 4 votes vote down vote up
@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;
}