0

我正在尝试将值从 rgb565 转换为 rgb888,但我似乎无法正确处理。我还检查了一些现有的答案,例如这个答案,但有些值似乎没有正确转换。

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

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;
}

使用此图像作为调色板:

单纯疱疹病毒

问题在于某些值:

rgb565转rgb888问题

为什么会这样?有没有办法解决这个问题?

4

2 回答 2

2

>>是算术移位,所以它会符号扩展值。>>>改用逻辑移位

在 C 中,没有>>>运算符,右移取决于实现,但如果变量是无符号的,大多数会进行逻辑移位,所以只需将其转换或定义为无符号

于 2013-07-28T05:09:02.760 回答
2

好的,如果我理解正确,这是解决方法:

在您的 wordToRGB() 方法中替换第一行:

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

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

当您从字节转换为整数时会出现问题。示例 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

无论你的最后一点是什么,处理左边的垫子,所以基本上在那之后,你的 | 扭曲了所有的价值观。

于 2013-07-24T23:52:15.683 回答