Java Code Examples for java.awt.image.BufferedImage.setRGB()

The following are Jave code examples for showing how to use setRGB() of the java.awt.image.BufferedImage class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jdk8u-jdk   File: PngOutputTypeTest.java   View Source Code Vote up 7 votes
private static ImageInputStream createTestImage(int type) throws IOException  {
    int w = 100;
    int h = 100;

    BufferedImage img = new BufferedImage(w, h, type);

    int dx = w / colors.length;

    for (int i = 0; i < colors.length; i++) {
        for (int x = i *dx; (x < (i + 1) * dx) && (x < w) ; x++) {
            for (int y = 0; y < h; y++) {
                img.setRGB(x, y, colors[i].getRGB());
            }
        }
    }

    File pwd = new File(".");
    File out = File.createTempFile("rgba_", ".png", pwd);
    System.out.println("Create file: " + out.getAbsolutePath());
    ImageIO.write(img, "PNG", out);
    return ImageIO.createImageInputStream(out);
}
 
Example 2
Project: Pixie   File: UtilsTest.java   View Source Code Vote up 6 votes
@Test ()
public void testCopySrcIntoDstAt4()
{
    System.out.println("copySrcIntoDstAt: send image with 1 pixel(RGB), expects 1 pixel(RGB)");

    BufferedImage src = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
    src.setRGB(0, 0, 10);

    BufferedImage dst = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);

    Utils.copySrcIntoDstAt(src, dst);

    DataBufferByte expected = (DataBufferByte) src.getRaster().getDataBuffer();
    DataBufferByte result = (DataBufferByte) dst.getRaster().getDataBuffer();

    assertArrayEquals(expected.getData(), result.getData());
}
 
Example 3
Project: Explorium   File: BlockStone.java   View Source Code Vote up 6 votes
@Override
public BufferedImage getTexture(double seed) {
	mainColor = new Color(128,128,128);
	
	BufferedImage bi = new BufferedImage(TextureGenerator.TEX_W, TextureGenerator.TEX_H, BufferedImage.TYPE_4BYTE_ABGR);
	Color c;
	for(int i = 0;i < bi.getWidth();i++) {
		for(int j = 0;j < bi.getHeight();j++) {
			c = TextureGenerator.fade(mainColor,TextureGenerator.trick(mainColor, -10),Math.abs(VMath.mod(i-j,32)-16)/16);

			//strength != 0 && weakStrength != 0 ? trick(mainc,turbulence(i+3.1f,j+3.1f,1,seed)*80) : mainc;
			bi.setRGB(i, j, c.getRGB());
		}
	}
	return bi;
}
 
Example 4
Project: Fetax-AI   File: CodeUtil.java   View Source Code Vote up 6 votes
private static BufferedImage createImage(String content, String imgPath,
        boolean needCompress) throws Exception {
    Hashtable hints = new Hashtable();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
    hints.put(EncodeHintType.MARGIN, 1);
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
            BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    BufferedImage image = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
                    : 0xFFFFFFFF);
        }
    }
    if (imgPath == null || "".equals(imgPath)) {
        return image;
    }
 // 插入图片
    CodeUtil.insertImage(image, imgPath, needCompress);
    return image;
}
 
Example 5
Project: openjdk-jdk10   File: Win32ShellFolder2.java   View Source Code Vote up 6 votes
private static Image makeIcon(long hIcon, boolean getLargeIcon) {
    if (hIcon != 0L && hIcon != -1L) {
        // Get the bits.  This has the side effect of setting the imageHash value for this object.
        final int[] iconBits = getIconBits(hIcon);
        if (iconBits != null) {
            // icons are always square
            final int size = (int) Math.sqrt(iconBits.length);
            final int baseSize = getLargeIcon ? 32 : 16;
            final BufferedImage img =
                    new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
            img.setRGB(0, 0, size, size, iconBits, 0, size);
            return size == baseSize
                    ? img
                    : new MultiResolutionIconImage(baseSize, img);
        }
    }
    return null;
}
 
Example 6
Project: Explorium   File: BlockDirt.java   View Source Code Vote up 6 votes
@Override
public BufferedImage getTexture(double seed) {
	mainColor = new Color(139,69,19);
	
	BufferedImage bi = new BufferedImage(TextureGenerator.TEX_W, TextureGenerator.TEX_H, BufferedImage.TYPE_4BYTE_ABGR);
	Color c;
	for(int i = 0;i < bi.getWidth();i++) {
		for(int j = 0;j < bi.getHeight();j++) {
			c = TextureGenerator.fade(mainColor,TextureGenerator.trick(mainColor,-40),Math.abs(VMath.mod(i-j,32)-16)/16);

			//strength != 0 && weakStrength != 0 ? trick(mainc,turbulence(i+3.1f,j+3.1f,1,seed)*80) : mainc;
			bi.setRGB(i, j, c.getRGB());
		}
	}
	return bi;
}
 
Example 7
Project: qrcode-utils   File: BufferedImageLuminanceSource.java   View Source Code Vote up 6 votes
public BufferedImageLuminanceSource(BufferedImage image, int left, int top,
                                    int width, int height) {
  super(width, height);

  int sourceWidth = image.getWidth();
  int sourceHeight = image.getHeight();
  if (left + width > sourceWidth || top + height > sourceHeight) {
    throw new IllegalArgumentException(
      "Crop rectangle does not fit within image data.");
  }

  for (int y = top; y < top + height; y++) {
    for (int x = left; x < left + width; x++) {
      if ((image.getRGB(x, y) & 0xFF000000) == 0) {
        image.setRGB(x, y, 0xFFFFFFFF);// = white
      }
    }
  }

  this.image = new BufferedImage(sourceWidth, sourceHeight,
    BufferedImage.TYPE_BYTE_GRAY);
  this.image.getGraphics().drawImage(image, 0, 0, null);
  this.left = left;
  this.top = top;
}
 
Example 8
Project: VASSAL-src   File: FileImageTypeConverter.java   View Source Code Vote up 6 votes
protected void read(InputStream in, BufferedImage dst) throws IOException {
  final int w = dst.getWidth();
  final int h = dst.getHeight();

  final byte[] bytes = new byte[4*w];
  final ByteBuffer bb = ByteBuffer.wrap(bytes);

  final int[] row = new int[w];

  for (int y = 0; y < h; ++y) {
    // read the row from the stream
    IOUtils.read(in, bytes);

    // convert the bytes to an int[]
    bb.asIntBuffer().get(row);

    // write the row back to the image
    dst.setRGB(0, y, w, 1, row, 0, w);
  }
}
 
Example 9
Project: JMathImgProc   File: ImageMgr.java   View Source Code Vote up 5 votes
public static BufferedImage convertBiMatrix2Img(byte[][] biMatrix, int nLeft, int nTop, int nWidth, int nHeight)	{
	if (biMatrix == null || biMatrix.length == 0 || biMatrix[0].length == 0)	{
		return null;	// we cannot create a zero width zero height bitmap.
	}
	int width = Math.min(nWidth, biMatrix.length - nLeft);
	int height = Math.min(nHeight, biMatrix[0].length - nTop);
	int[] colorMatrix = new int[width * height];
	for (int idx = 0; idx < height; idx ++)	{
		for (int idx1 = 0; idx1 < width; idx1 ++)	{
			if (biMatrix[idx1 + nLeft][idx + nTop] == 0)	{
				colorMatrix[idx * width + idx1] = Color.WHITE.getRGB();
			} else	{
				colorMatrix[idx * width + idx1] = Color.BLACK.getRGB();
			}
		}
	}

       // Initialize BufferedImage, assuming Color[][] is already properly populated.
       BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

       // Set each pixel of the BufferedImage to the color from the Color[][].
       for (int x = 0; x < width; x++) {
           for (int y = 0; y < height; y++) {
               bufferedImage.setRGB(x, y, colorMatrix[y * width + x]);
           }
       }		
	return bufferedImage;
}
 
Example 10
Project: imagetozxspec   File: GigaScreenConverterImpl.java   View Source Code Vote up 5 votes
@Override
public ResultImage[] convert(BufferedImage original) {
    OptionsObject oo = OptionsObject.getInstance();
    BufferedImage output = ImageHelper.copyImage(original);
    final BufferedImage output1 = new BufferedImage(output.getWidth(), output.getHeight(), BufferedImage.TYPE_INT_ARGB);
    final BufferedImage output2 = new BufferedImage(output.getWidth(), output.getHeight(), BufferedImage.TYPE_INT_ARGB);

    // Dithers the images to the GigaScreen palette (if/else to switch between preview window images and actual output)
    ResultImage[] resultImage = imageConverter.convert(output);
    output = resultImage[0].getImage();

    // Algorithm replaces each pixel with the colour from the closest matching
    // 4 colour GigaScreen attribute block.
    GigaScreenAttribute[][] quad = getGigaScreenAttribute(output);
    GigaScreenAttribute combo = null;
    for (int y = 0; y < output.getHeight(); ++y) {
        for (int x = 0; x < output.getWidth(); ++x) {
            if (x % ATTRIBUTE_BLOCK_SIZE == 0) {
                combo = quad[x / ATTRIBUTE_BLOCK_SIZE][y / ATTRIBUTE_BLOCK_SIZE];
            }
            GigaScreenAttribute.GigaScreenColour c = ColourHelper.getClosestGigaScreenColour(output.getRGB(x, y), combo);
            output.setRGB(x, y, c.getGigascreenColour());
            output1.setRGB(x, y, c.getScreen1Colour());
            output2.setRGB(x, y, c.getScreen2Colour());
        }
    }

    if (oo.getExportTape() || oo.getExportScreen()) {
        orderByGigaScreenPaletteOrder(output1, output2);
    }

    return new ResultImage[]{new ResultImage(ResultImageType.FINAL_IMAGE, output),
            new ResultImage(ResultImageType.SUPPORTING_IMAGE, output1),
            new ResultImage(ResultImageType.SUPPORTING_IMAGE, output2)};
}
 
Example 11
Project: openjdk-jdk10   File: bug8016833.java   View Source Code Vote up 5 votes
/**
 * Subtracts img2 from img1
 */
BufferedImage subImages(BufferedImage img1, BufferedImage img2) {
    if (img1.getHeight() != img2.getHeight() ||
            img1.getWidth() != img2.getWidth()) {
        throw new RuntimeException("Different sizes");
    }
    BufferedImage ret = new BufferedImage(img1.getWidth(), img1.getHeight(), img1.getType());

    for (int x = 0; x < ret.getWidth(); x++) {
        for (int y = 0; y < ret.getHeight(); y++) {
            ret.setRGB(x, y, subPixels(img1.getRGB(x, y), img2.getRGB(x, y)));
        }
    }
    return ret;
}
 
Example 12
Project: openjdk-jdk10   File: ImageTool.java   View Source Code Vote up 5 votes
/**
 * Subtracts subimage from image. Could be used to save file difference for
 * future analysis.
 *
 * @param minuend an image to subtract from.
 * @param deduction an image to subtract.
 * @param relativeX - deduction-in-minuend X coordinate
 * @param relativeY - deduction-in-minuend Y coordinate
 * @return a result image.
 */
public static BufferedImage subtractImage(BufferedImage minuend, BufferedImage deduction, int relativeX, int relativeY) {
    int mWidth = minuend.getWidth();
    int mHeight = minuend.getHeight();
    int dWidth = deduction.getWidth();
    int dHeight = deduction.getHeight();

    int maxWidth = (mWidth > relativeX + dWidth) ? mWidth : (relativeX + dWidth);
    int maxHeight = (mHeight > relativeY + dHeight) ? mHeight : (relativeY + dHeight);

    BufferedImage result = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
    int mColor, dColor;
    for (int x = 0; x < maxWidth; x++) {
        for (int y = 0; y < maxHeight; y++) {
            if (x >= mWidth
                    || y >= mHeight) {
                mColor = 0;
            } else {
                mColor = minuend.getRGB(x, y);
            }
            if (x >= dWidth + relativeX
                    || y >= dHeight + relativeY
                    || x < relativeX
                    || y < relativeY) {
                dColor = 0;
            } else {
                dColor = deduction.getRGB(x - relativeX, y - relativeY);
            }
            result.setRGB(x, y, subtractColors(mColor, dColor));
        }
    }
    return result;
}
 
Example 13
Project: incubator-netbeans   File: ColorUtil.java   View Source Code Vote up 5 votes
/**
 * Dynamically creates and returns drag texture icon
 */
private static final Icon initVistaDragTextureImage() {
    BufferedImage i = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
    int grey = new Color(124,124,124).getRGB();
    i.setRGB(1, 0, grey);
    i.setRGB(0, 1, grey);
    i.setRGB(0, 0, new Color(162,163,164).getRGB());
    i.setRGB(1, 1, new Color(107,107,107).getRGB());
    return new ImageIcon(i);
}
 
Example 14
Project: cuttlefish   File: JPEGExporter.java   View Source Code Vote up 5 votes
private void exportData() throws IOException {
	PreviewController controller = Lookup.getDefault().lookup(
			PreviewController.class);
	PreviewProperties props = controller.getModel(workspace)
			.getProperties();

	props.putValue(PreviewProperty.VISIBILITY_RATIO, 1.0);
	props.putValue("width", width);
	props.putValue("height", height);
	props.putValue(PreviewProperty.MARGIN, margin);
	// props.putValue(PreviewProperty.EDGE_CURVED, false);

	controller.refreshPreview(workspace);

	ProcessingTarget target = (ProcessingTarget) controller
			.getRenderTarget(RenderTarget.PROCESSING_TARGET, workspace);

	target.refresh();

	PGraphicsJava2D pg2 = (PGraphicsJava2D) target.getGraphics();
	BufferedImage img = new BufferedImage(width, height,
			BufferedImage.TYPE_INT_RGB);
	img.setRGB(0, 0, width, height, pg2.pixels, 0, width);
	ImageIO.write(img, "jpg", outputStream);

	outputStream.close();
	props.removeSimpleValue(PreviewProperty.VISIBILITY_RATIO);
	props.removeSimpleValue("width");
	props.removeSimpleValue("height");
	props.removeSimpleValue(PreviewProperty.MARGIN);
}
 
Example 15
Project: imagetozxspec   File: LowErrorAtkinsonDitherStrategy.java   View Source Code Vote up 5 votes
public void distributeError(BufferedImage output, int oldPixel, int newPixel, int x, int y, Integer boundX, Integer boundY) {
	if (isInBounds(output, x+1, y, boundX, boundY)) {output.setRGB(x+1, y, calculateAdjustedRGB(oldPixel, newPixel, output.getRGB(x+1, y), TWENTY_FOURTH));}
	if (isInBounds(output, x+2, y, boundX, boundY)) {output.setRGB(x+2, y, calculateAdjustedRGB(oldPixel, newPixel, output.getRGB(x+2, y), TWENTY_FOURTH));}
	if (isInBounds(output, x-1, y+1, boundX, boundY)) {output.setRGB(x-1, y+1, calculateAdjustedRGB(oldPixel, newPixel, output.getRGB(x-1, y+1), TWENTY_FOURTH));}
	if (isInBounds(output, x, y+1, boundX, boundY)) {output.setRGB(x, y+1, calculateAdjustedRGB(oldPixel, newPixel, output.getRGB(x, y+1), TWENTY_FOURTH));}
	if (isInBounds(output, x+1, y+1, boundX, boundY)) {output.setRGB(x+1, y+1, calculateAdjustedRGB(oldPixel, newPixel, output.getRGB(x+1, y+1), TWENTY_FOURTH));}
	if (isInBounds(output, x, y+2, boundX, boundY)) {output.setRGB(x, y+2, calculateAdjustedRGB(oldPixel, newPixel, output.getRGB(x, y+2), TWENTY_FOURTH));}
}
 
Example 16
Project: os   File: MatrixToImageWriter.java   View Source Code Vote up 5 votes
/**
 * As {@link #toBufferedImage(com.google.zxing.common.BitMatrix)}, but allows customization of the output.
 */
public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfig config) {
  int width = matrix.getWidth();
  int height = matrix.getHeight();
  BufferedImage image = new BufferedImage(width, height, config.getBufferedImageColorModel());
  int onColor = config.getPixelOnColor();
  int offColor = config.getPixelOffColor();
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor);
    }
  }
  return image;
}
 
Example 17
Project: Backmemed   File: ScreenShotHelper.java   View Source Code Vote up 5 votes
public static BufferedImage createScreenshot(int width, int height, Framebuffer framebufferIn)
{
    if (OpenGlHelper.isFramebufferEnabled())
    {
        width = framebufferIn.framebufferTextureWidth;
        height = framebufferIn.framebufferTextureHeight;
    }

    int i = width * height;

    if (pixelBuffer == null || pixelBuffer.capacity() < i)
    {
        pixelBuffer = BufferUtils.createIntBuffer(i);
        pixelValues = new int[i];
    }

    GlStateManager.glPixelStorei(3333, 1);
    GlStateManager.glPixelStorei(3317, 1);
    pixelBuffer.clear();

    if (OpenGlHelper.isFramebufferEnabled())
    {
        GlStateManager.bindTexture(framebufferIn.framebufferTexture);
        GlStateManager.glGetTexImage(3553, 0, 32993, 33639, pixelBuffer);
    }
    else
    {
        GlStateManager.glReadPixels(0, 0, width, height, 32993, 33639, pixelBuffer);
    }

    pixelBuffer.get(pixelValues);
    TextureUtil.processPixelValues(pixelValues, width, height);
    BufferedImage bufferedimage = new BufferedImage(width, height, 1);
    bufferedimage.setRGB(0, 0, width, height, pixelValues, 0, width);
    return bufferedimage;
}
 
Example 18
Project: zheng-lite   File: CaptchaUtil.java   View Source Code Vote up 4 votes
private void creatImage() {
	int fontWidth = width / codeCount;// 字体的宽度
	int fontHeight = height - 5;// 字体的高度
	int codeY = height - 8;

	// 图像buffer
	buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	Graphics g = buffImg.getGraphics();
	//Graphics2D g = buffImg.createGraphics();
	// 设置背景色
	g.setColor(getRandColor(200, 250));
	g.fillRect(0, 0, width, height);


	// 设置字体
	//Font font1 = getFont(fontHeight);
	Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
	g.setFont(font);

	// 设置干扰线
	for (int i = 0; i < lineCount; i++) {
		int xs = random.nextInt(width);
		int ys = random.nextInt(height);
		int xe = xs + random.nextInt(width);
		int ye = ys + random.nextInt(height);
		g.setColor(getRandColor(1, 255));
		g.drawLine(xs, ys, xe, ye);
	}

	// 添加噪点
	float yawpRate = 0.01f;// 噪声率
	int area = (int) (yawpRate * width * height);
	for (int i = 0; i < area; i++) {
		int x = random.nextInt(width);
		int y = random.nextInt(height);

		buffImg.setRGB(x, y, random.nextInt(255));
	}

	String str1 = randomStr(codeCount);// 得到随机字符
	this.code = str1;
	for (int i = 0; i < codeCount; i++) {
		String strRand = str1.substring(i, i + 1);
		g.setColor(getRandColor(1, 255));
		// g.drawString(a,x,y);
		// a为要画出来的东西,x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处

		g.drawString(strRand, i * fontWidth + 3, codeY);
	}
}
 
Example 19
Project: -Spring-SpringMVC-Mybatis-   File: CaptchaUtil.java   View Source Code Vote up 4 votes
private void creatImage() {
	int fontWidth = width / codeCount;// 字体的宽度
	int fontHeight = height - 5;// 字体的高度
	int codeY = height - 8;

	// 图像buffer
	buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	Graphics g = buffImg.getGraphics();
	//Graphics2D g = buffImg.createGraphics();
	// 设置背景色
	g.setColor(getRandColor(200, 250));
	g.fillRect(0, 0, width, height);


	// 设置字体
	//Font font1 = getFont(fontHeight);
	Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
	g.setFont(font);

	// 设置干扰线
	for (int i = 0; i < lineCount; i++) {
		int xs = random.nextInt(width);
		int ys = random.nextInt(height);
		int xe = xs + random.nextInt(width);
		int ye = ys + random.nextInt(height);
		g.setColor(getRandColor(1, 255));
		g.drawLine(xs, ys, xe, ye);
	}

	// 添加噪点
	float yawpRate = 0.01f;// 噪声率
	int area = (int) (yawpRate * width * height);
	for (int i = 0; i < area; i++) {
		int x = random.nextInt(width);
		int y = random.nextInt(height);

		buffImg.setRGB(x, y, random.nextInt(255));
	}

	String str1 = randomStr(codeCount);// 得到随机字符
	this.code = str1;
	for (int i = 0; i < codeCount; i++) {
		String strRand = str1.substring(i, i + 1);
		g.setColor(getRandColor(1, 255));
		// g.drawString(a,x,y);
		// a为要画出来的东西,x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处

		g.drawString(strRand, i * fontWidth + 3, codeY);
	}
}
 
Example 20
Project: jdk8u-jdk   File: Win32ShellFolderManager2.java   View Source Code Vote up 3 votes
private static Image getStandardViewButton(int iconIndex) {
    Image result = STANDARD_VIEW_BUTTONS[iconIndex];

    if (result != null) {
        return result;
    }

    BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);

    img.setRGB(0, 0, 16, 16, Win32ShellFolder2.getStandardViewButton0(iconIndex), 0, 16);

    STANDARD_VIEW_BUTTONS[iconIndex] = img;

    return img;
}