gpt4 book ai didi

java - 如何正确从rgb565转换为rgb888

转载 作者:行者123 更新时间:2023-12-01 18:45:50 27 4
gpt4 key购买 nike

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

这是我迄今为止尝试过的:

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

使用此图像作为调色板:

hsv

问题出在某些值上:

rgb565 to rgb888 issue

为什么会发生这种情况?有办法解决吗?

最佳答案

好的,如果我理解正确的话,这里是修复:

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

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

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

当您从 byte 转换为 int 时,就会出现问题。示例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

处理左边的垫与你的最后一点是什么,所以本质上在那之后,你的|扭曲了所有的值(value)观。

关于java - 如何正确从rgb565转换为rgb888,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17842164/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com