This draws a pixel-array onto another pixel-array. I get around 2200 fps on an i5
public void draw(ArrayImage a, int xp, int yp) { //Draw a normal tile
for (int y = 0; y < a.height; y++) {
int yPixel = y + yp;
if (yPixel < 0 || yPixel >= height) continue;
for (int x = 0; x < a.width; x++) {
int xPixel = x + xp;
if (xPixel < 0 || xPixel >= width) continue;
int srcPixel = a.pixels[x + y * a.width];
if (srcPixel < 0 /*if <0 srcPixel is meant to be transparent*/) pixels[xPixel + yPixel * width] = srcPixel;
}
}
}
Why not just use System.arraycopy to copy a region of your image?
/**
* Quickly copies the specified pixels from the source image to the destination image.
* @param src the source image
* @param dst the destination image
* @param srcX the x position of the source image to start copying
* @param srcY the y position of the source image to start copying
* @param srcW the width of the section to copy from source
* @param srcH the height of the section to copy from source
* @param dstX the x position to place the copied pixels at on the destination image
* @param dstY the y position to place the copied pixels at on the destination image
*/
public void copyPixels(BufferedImage src, BufferedImage dst, int srcX, int srcY, int srcW, int srcH, int dstX, int dstY) {
int[] srcbuf = ((DataBufferInt)src.getRaster().getDataBuffer()).getData();
int[] dstbuf = ((DataBufferInt)dst.getRaster().getDataBuffer()).getData();
int srcOff = srcX + srcY * src.getWidth();
int dstOff = dstX + dstY * dst.getWidth();
for (int y=0; y<srcH; y++) { //copy every row of src starting at offset
System.arraycopy(srcbuf, srcOff, dstbuf, dstOff, srcW);
srcOff += src.getWidth();
dstOff += dst.getWidth();
}
}
EDIT: If you’re loading images with ImageIO, they may not be TYPE_INT_ARGB. You can convert them easily like so:
if (image.getType()!=BufferedImage.TYPE_INT_ARGB) {
BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
convertedImage.createGraphics().drawRenderedImage(image, null);
image = convertedImage;
}
My method wasn’t intended to be used every frame. It’s just a quick way to copy pixels from one image to another, without dealing with volatile images / video memory / etc.
You’ve changed your questions so many times that I’m no longer really sure what your current problem is or what you’re trying to accomplish…
[quote]I’m trying to do pixel-by-pixel rendering to an image using for loops. I have an int array where pixel data is written to, and then, once per frame, that array is rendered to the BufferedImage.
[/quote]
BufferStrategy s = getBufferStrategy();
if (s == null) {
createBufferStrategy(2);
}
for (int i = 0; i < pixels.length; i++) {
pixels[i] = i + (int)System.currentTimeMilllis();
}
drawImageAt(10, 20, someImage);
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
bs.show();
public void drawImageAt(int x, int y, BufferedImage img) {
int[] pixies = img.getRGB(0,0, img.getWidth(), img.getHeight(), null, 0, img.getWidth() );
for (int yy = 0; yy < img.getHeight(); yy++) {
for (int xx = 0 ; xx < img.getWidth(); xx++) {
pixels[ (y+yy) * image.getWidth() + x + xx ] = pixies[y*img.getWidth() + x];
}
}
}