【问题标题】:RGB888 to RGB565 and vice versa causing loss of info?RGB888 到 RGB565,反之亦然导致信息丢失?
【发布时间】:2014-01-14 11:43:52
【问题描述】:

我有以下代码可以将 RGB565 转换为 RGB888,反之亦然:

public static void main(String[] args) {
    // TODO code application logic here
    System.out.println(Integer.toHexString(RGB888ToRGB565(0x11ffffff)));
    System.out.println(Integer.toHexString(RGB565ToRGB888(RGB888ToRGB565(0x7FC9FF))));
}

static int RGB888ToRGB565(int red, int green, int blue) {
    final int B = (blue >>> 3) & 0x001F;
    final int G = ((green >>> 2) << 5) & 0x07E0;
    final int R = ((red >>> 3) << 11) & 0xF800;

    return (R | G | B);
}

static int RGB888ToRGB565(int aPixel) {
    //aPixel <<= 8;
    //System.out.println(Integer.toHexString(aPixel));
    final int red = (aPixel >> 16) & 0xFF;
    final int green = (aPixel >> 8) & 0xFF;
    final int blue = (aPixel) & 0xFF;
    return RGB888ToRGB565(red, green, blue);
}

static int RGB565ToRGB888(int aPixel) {
    final int b = (((aPixel) & 0x001F) << 3) & 0xFF;
    final int g = (((aPixel) & 0x07E0) >>> 2) & 0xFF;
    final int r = (((aPixel) & 0xF800) >>> 8) & 0xFF;
    // return RGBA
    return 0x000000ff | (r << 24) | (g << 16) | (b << 8);
}

问题出在第二行,当它转换回 rgb888 时,我丢失了颜色信息。有谁知道更多关于位移和掩码的人可以帮助我吗?

提前致谢!

【问题讨论】:

  • 当然有损失。为什么你甚至会期望没有损失?
  • 是的,我有,但并没有真正帮助。我知道应该有损失,但我不认为会有那么大的损失。
  • 您的问题是 1)颜色变得不那么详细还是 2)颜色因不正确的转换而损坏?如果是前者,那是显而易见且不可避免的。 (通过从 rgb888 转换为 rgb565 并返回,您实际上是在破坏三分之一的数据,将可能的颜色数量减少到原来的 1/256)。它无法通过任何方式恢复。)如果是后者,您能否提供一个示例说明它是如何损坏的?
  • 不,颜色实际上已完全损坏,有时它返回的值甚至不接近原始值。

标签: java colors rgb


【解决方案1】:

这条线是不是错了?

final int g = (((aPixel) & 0x07E0) >>> 2) & 0xFF;

应该是:

final int g = (((aPixel) & 0x07E0) >>> 3) & 0xFF;

在我看来,您的其余代码看起来还不错。我不知道这是否解释了它。否则你可能不得不根据什么测试来定义什么是“那么多损失”。

【讨论】:

  • 嗯,不,这只是导致它返回更不正确的输出。
  • 嗯,线路错了,对吧?该值在 +5 移位处,因此您需要将其移位 -3 以从 8 位字节的顶部开始。你还没有说是什么问题。什么输出与预期输出不匹配。
【解决方案2】:

static int RGB888ToRGB565(int aPixel) 中,您期望 ARGB(或只是 RGB)作为输入

static int RGB565ToRGB888(int aPixel) 中,您将 RGBA 作为输出返回

在同一代码中使用 ARGB 和 RGBA 似乎不合逻辑。我猜你是不是在之后比较颜色时弄混了?

除了这个 Sean Owens 上面的评论是正确的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2014-11-08
    • 2012-02-16
    • 2013-08-16
    • 2012-06-27
    • 2017-02-05
    • 2012-12-01
    相关资源
    最近更新 更多