字节数组转换为8位真彩色图像



我正试图从一个旧的PC游戏中抓取一些精灵。我找到了精灵,并将它们撕成了单独的灰度文件。现在我正在努力研究如何给它们上色。可执行文件或其数据文件中似乎没有任何调色板数据,再加上游戏所需的颜色深度(256色),我相信每个字节实际上都是一个8位的真彩色值。

假设我有一个(在这种情况下缩短)数组,看起来是这样的:

12 11 12 11
0A 13 12 11
13 12 11 0A
12 11 13 13

一些类似于我用来写图像的示例代码看起来是这样的:

DataInputStream dis = new DataInputStream(new FileInputStream("GAME.EXE"));
dis.skipBytes(bytesToImage);
BufferedImage bufferedImage = new BufferedImage(columns, rows, BufferedImage.TYPE_BYTE_INDEXED);
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < columns; j++) {
        byte pixel = dis.readByte();
        System.out.print(toHexString(pixel));
        //For now, greyscale everything.
        int value = pixel << 16 | pixel << 8 | pixel;
        bufferedImage.setRGB(j, i, value);
    }
    System.out.print(System.getProperty("line.separator"));
}
ImageIO.write(bufferedImage, "PNG", new File("test.png"));

我把传递给构造函数的imageType和手动传递ColorModel都搞砸了,但似乎都没有做任何有用的事情。对像素进行二进制"与"运算并进行一些位偏移,通常只会将图像设置为深红色。

在这种情况下,我该如何将每个像素设置为正确的真彩色值?

8位真彩色表示第7位到第5位包含红色,第4位到第2位包含绿色,第1位和第0位包含蓝色。(即RRRGGGBB)您需要将这些移位到24位真彩色模型中的高阶位。

使用具有24位RGB颜色的颜色模型,如TYPE_INT_RGB或TYPE_INT_ARGB,并从8位值中导出24位像素值:

int pixel24 = 
    ((pixel & (128+64+32)) << (16+5-5)) |
    ((pixel & (16+8+4))    << (8+5-2))  |
    ((pixel & (2+1))       << 6);

换句话说:

int pixel24 = 
    ((pixel & 224) << 16) | ((pixel & 28) << 11) | ((pixel & 3) << 6);

int pixel24 = 
    ((pixel & 0xE0) << 16) | ((pixel & 0x1C) << 11) | ((pixel & 0x03) << 6);

最新更新