如何正确从rgb565转换为rgb888 [英] How to correctly convert from rgb565 to rgb888

查看:1807
本文介绍了如何正确从rgb565转换为rgb888的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个值从rgb565转换为rgb888,但我似乎不能得到它的权利。
我也检查了一些现有的答案,如这一个但有一些值似乎没有正确转换。

I'm trying to convert a value from rgb565 to rgb888 but I can't seem to get it right. I've also checked a few existing answers like this one but there are values that don't seem to be properly convert.

这里是我到目前为止尝试过的:

Here's what I've tried so far:

PImage picker;
int c;
void setup(){
  size(285,240);
  picker = loadImage("hsv.gif");
  int c = color(255,255,255);
  byte[] word = colorToWord(c);
  int[] rgb = wordToRGB(word);

  noStroke();
  fill(c);
  rect(0,0,50,100);
  fill(rgb[0],rgb[1],rgb[2]);
  rect(50,0,50,100);
}
void draw(){
  image(picker,0,0);
  if((mouseX >= 0 && mouseX <= picker.width)&&
     (mouseY >= 0 && mouseY <= picker.height)) c = picker.get(mouseX,mouseY);

  byte[] word = colorToWord(c);
  int[] rgb = wordToRGB(word);
//  int[] rgb = rgbWordToRGB(word);
  int cc = color(rgb[0],rgb[1],rgb[2]);

  fill(c);
  rect(0,159,142,80);
  fill(cc);
  rect(142,159,143,80);
  fill(0);
  text("original\n"+hex(c),10,185);
  text("converted\n"+hex(cc),152,185);
}

byte[] colorToWord(int c){
  int r = (c >> 16) & 0xFF;
  int g = (c >> 8)  & 0xFF;
  int b =  c        & 0xFF;
  return new byte[]{(byte)((r&248)|g>>5),(byte)((g&28)<<3|b>>3)};
}
int[] wordToRGB3(byte[] data){
// Reconstruct 16 bit rgb565 value from two bytes
  int rgb565 = (data[0] & 255) | ((data[1] & 255) << 8);

  // Extract raw component values (range 0..31 for g and b, 0..63 for g)  
  int b5 = rgb565 & 0x1f;
  int g6 = (rgb565 >> 5) & 0x3f;
  int r5 = (rgb565 >> 11) & 0x1f;

  // Scale components up to 8 bit: 
  // Shift left and fill empty bits at the end with the highest bits,
  // so 00000 is extended to 000000000 but 11111 is extended to 11111111
  int b = (b5 << 3) | (b5 >> 2);
  int g = (g6 << 2) | (g6 >> 4);
  int r = (r5 << 3) | (r5 >> 2); 
  return new int[]{r,g,b};
}
int[] wordToRGB2(byte[] word){
  int r = word[0]&248;
  int g = (word[0]<<5) | ((word[1]&28)>>3);
  int b = word[1] << 3;
  r <<= 3;
  g <<= 2;
  b <<= 3;
  return new int[]{r,g,b};
}
int[] wordToRGB(byte[] word){
  int c = (word[0] << 8) | (word[1]);
  //RGB565
  int r = (c >> (6+5)) & 0x01F;
  int g = (c >> 5) & 0x03F;
  int b = (c) & 0x01F;
  //RGB888 - amplify
  r <<= 3;
  g <<= 2;
  b <<= 3; 
  return new int[]{r,g,b};
}
final int RGB565_MASK_RED       = 0xF800;
final int RGB565_MASK_GREEN     = 0x07E0;  
final int RGB565_MASK_BLUE      = 0x001F; 

int[] rgbWordToRGB(byte[] word){
 int rgb565 = (word[0] << 8) | (word[1]);
 int[] rgb24 = new int[3];
 rgb24[2] = (rgb565 & RGB565_MASK_RED) >> 11;     
 rgb24[1] = (rgb565 & RGB565_MASK_GREEN) >> 5;  
 rgb24[0] = (rgb565 & RGB565_MASK_BLUE);  

 //amplify the image  
 rgb24[2] <<= 3;  
 rgb24[1] <<= 2;  
 rgb24[0] <<= 3; 
 return rgb24;
}

使用此图片作为调色板:

using this image as a colour palette:

问题在于某些值:

为什么会发生这种情况?有没有办法解决这个问题?

Why does that happen ? Is there a way to fix that ?

推荐答案

确定所以这里的修复如果我理解正确:

ok so here's the fix if I understand correctly:

替换第一行:

int c = (word[0] << 8) | (word[1]);

  int c = (word[0] << 8) | (word[1] & 0xFF);

当从byte转换为int时,会出现问题。示例1:

The problem happens when you convert from byte to int. Example 1:

int a = 255;
println(binary(a));

int b = (byte) 255;
println(binary(b));

列印:

00000000000000000000000011111111
11111111111111111111111111111111

示例2:

int a = 127;
println(binary(a));

int b = (byte) 127;
println(binary(b));

列印:

00000000000000000000000001111111
00000000000000000000000001111111

位,所以基本上之后,你的扭曲所有的值。

Processing pads with to the left with whatever your last bit was, so essentially after that, your | distorts all the values.

这篇关于如何正确从rgb565转换为rgb888的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆