2014-02-10 74 views
-1

有什么办法在使用INT_RGB的BufferedImage中使用alpha?我正在使用一维像素数组将精灵渲染到屏幕上,但我希望能够使用Alpha。有没有什么方法可以混合颜色,并在Photoshop中实现某种图层系统?Java:BufferedImage INT_RGB Alpha?

我一直在尝试通过混合颜色来创建一些自定义alpha,但我不太清楚该怎么做。

这是我到目前为止有:

的BufferedImage &像素阵列:

public static void drawSprite(Sprite sprite, int coord_x, int coord_y) { 
    int boundsX = (coord_x + sprite.width), 
     boundsY = (coord_y + sprite.height), 
     index = -1, pixels[] = Screen.pixels; 

    for(int y = coord_y; y < boundsY; y++) { 
     for(int x = coord_x; x < boundsX; x++) { 

      index++; 

      if(Screen.pixels[x + y * width] == 0) { 
       Screen.pixels[x + y * width] = sprite.pixels[index];  

      } else { 
       int[] screenPixel = intToARGB(Screen.pixels[x + y * width]); 
       int[] spritePixel = intToARGB(sprite.pixels[index]); 
       int[] newPixel = new int[4]; 

       newPixel[0] = (screenPixel[0] + spritePixel[0])/2; 
       newPixel[1] = (screenPixel[1] + spritePixel[1])/2; 
       newPixel[2] = (screenPixel[2] + spritePixel[2])/2; 
       newPixel[3] = (screenPixel[3] + spritePixel[3])/2; 

       Screen.pixels[x + y * width] = Integer.parseInt((Integer.toString(newPixel[0]) + 
                   Integer.toString(newPixel[1]) + 
                   Integer.toString(newPixel[2]) + 
                   Integer.toString(newPixel[3]))); 
      } 

     } 
    } 
} 
+1

注意TYPE_INT_RGB中缺少A :)为什么不使用TYPE_INT_ARGB? – slipperyseal

+0

它使用了更多的资源,它也不适合我的像素阵列。我想尽可能使用RGB –

+0

实际上,它不使用32位(4字节)整数,就像ARGB一样。它只是不使用最后一个字节。 “将资源节省下来”从来不合理地推动一个方形钉入圆孔。如果有的话,解决方法将变得更慢,更复杂 – slipperyseal

回答

0

有没有办法使用alpha:用于渲染精灵

BufferedImage image = new BufferedImage(width/scale, height/scale, BufferedImage.TYPE_INT_RGB); 
int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

方法在使用INT_RGBBufferedImage

简短的回答:

龙答:没有,INT_RGB类型的BufferedImage不包含Alpha(除非你重新定义R,G和B表示,这是...)。 但它当然可以正确地构成其他类型的BufferedImage用α(如TYPE_INT_ARGBTYPE_4BYTE_ABGR或甚至与α或在IndexColorModel透明像素TYPE_BYTE_INDEXED)到使用TYPE_INT_RGB一个BufferedImage也许这就是你想要做的?注意:Java2D已经在类AlhpaComposite中实现了图像合成和不同类型的(Porter/Duff)alpha混合规则。使用这个类,你可能会得到硬件加速的alpha混合,这是超快的。我不明白你为什么要重新实施这个。