Android的bitmap.setPixel(X,Y,颜色)在值设置传递 [英] Android bitmap.setPixel(x,y,color) is setting passed in value
问题描述
创建使用Bitmap.create(25,25,Config.ARGB_8888)位图
Create a bitmap using Bitmap.create(25, 25, Config.ARGB_8888)
设置像素具有α值小于或等于0xA9的结果没有被设定什么传入的像素我读到另一个堆栈溢出问题,说要setHasAlpha(真),我在我的测试一样 - 但这仍然没有解决问题。
Setting a pixel with an alpha value less than or equal to 0xA9 results in the pixel not being set with what's passed in. I read another stack overflow question saying to setHasAlpha(true), which I did in my test -- but that still didn't fix the issue.
下面是显示我的问题,我的Android测试用例:
Here's my android test case showing my issue:
public void testSettingBitmaps() {
Bitmap bitmap = Bitmap.createBitmap(25, 25, Config.ARGB_8888);
bitmap.setHasAlpha(true);
int color = 0x00fefefe;
int x= 0;
int y = 0;
for(int alpha = 0xFF000000; alpha != 0x00000000; alpha = alpha - 0x01000000) {
int colorPlusAlpha = color + alpha;
bitmap.setPixel(x, y, colorPlusAlpha);
//
// This test succeeds if the bitmap let us set the pixel.
//
assertEquals(String.format("Current alpha value: %x, Expected pixel value: %x, Actual pixel value: %x", alpha, colorPlusAlpha, bitmap.getPixel(x, y)),
colorPlusAlpha, bitmap.getPixel(x, y));
}
}
这code失败,出现以下的输出:junit.framework.AssertionFailedError:当前alpha值:a9000000,预计像素值:a9fefefe,实际像素值:a9fdfdfd预期:其中,-1442906370>却被:LT; - 1442972163>
This code fails with the following output:junit.framework.AssertionFailedError: Current alpha value: a9000000, Expected pixel value: a9fefefe, Actual pixel value: a9fdfdfd expected:<-1442906370> but was:<-1442972163>
推荐答案
这实际上是按预期工作。
This actually works as intended.
的问题是,位图被存储在premultiplied阿尔法格式。这意味着,当你设置一个像素值0xa9fefefe,存储的值实际上是0xa9a8a8a8(0xa9 * 0xFE的/ 255 = 0xa8)。然后当你调用与getPixel(),存储的值是非premultiplied。与舍入误差您获得0xa9fdfdfd。
The problem is that Bitmaps are stored in premultiplied alpha format. This means that when you set a pixel value to 0xa9fefefe, the stored value is actually 0xa9a8a8a8 (0xa9*0xfe/255=0xa8.) Then when you call getPixel(), the stored value is "un-premultiplied." With rounding errors you get 0xa9fdfdfd.
下面是一个故障,传递0xa9fefefe的ARGB值。每个RGB字节将由alpha值在存储前成倍增加。为了简化,我们将看看只有红色字节:
Here is a breakdown, you pass an ARGB value of 0xa9fefefe. Each RGB byte will be multiplied by the alpha value before being stored. To simplify we'll look at the red byte only:
R = 169 *255分之254
R = 168(或0xa8)
R=169*254/255 R=168 (or 0xa8)
然后当你调用与getPixel(),将RGB字节由阿尔法分为:
Then when you call getPixel(), the RGB bytes are divided by the alpha:
R = 255 *169分之168
R = 253(或者是0xFD)
R=255*168/169 R=253 (or 0xfd)
这篇关于Android的bitmap.setPixel(X,Y,颜色)在值设置传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!