简体   繁体   English

getRGB和getRaster带来不同的结果

[英]getRGB and getRaster brings different results

If I create an BufferedImage in that way: 如果我以这种方式创建BufferedImage

    BufferedImage image = new BufferedImage(4, 3, BufferedImage.TYPE_BYTE_GRAY);
    image.getRaster().setPixels(0, 0, image.getWidth(), image.getHeight(), new int[]
                    {
                            0, 255, 192, 183,
                            83, 143, 52, 128,
                            102, 239, 34, 1
                    }
    );

And then when get pixels values using getRGB method: 然后,当使用getRGB方法获取像素值时:

    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            System.out.print((image.getRGB(x, y)) & 0xFF);
            System.out.print(" ");
        }
        System.out.println();
    }

I see result that differ from input: 我看到与输入不同的结果:

0 255 225 220 
155 197 125 188 
170 248 102 13 

I could get original values using image.getRaster().getDataBuffer() , but why getRGB results are different? 我可以使用image.getRaster().getDataBuffer()获得原始值,但是为什么getRGB结果不同?

You are directly writing pixels to the Raster, but you go through the Image API for getting them back. 您是直接将像素写入Raster,但是要通过Image API来获取像素。

These are very different API's, the Raster works with raw pixel data, while the Image API takes the ColorModel into account. 这些是非常不同的API,Raster处理原始像素数据,而Image API考虑了ColorModel。 When you call getRGB(), the call is delegated through the ColorModel, so some nonlinear conversion between the sRGB color space and the Raster's color space can be/is performed. 调用getRGB()时,该调用是通过 ColorModel委托的,因此可以执行sRGB颜色空间和Raster的颜色空间之间的一些非线性转换。

If you try to convert them backwards: 如果您尝试将它们向后转换:

    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            int rgb = image.getRGB(x, y);
            image.setRGB(x, y, rgb);
        }
    }

in Raster data you will see results that are very close to original: Raster数据中,您将看到非常接近原始结果:

0 255 192 183 
84 142 52 128 
103 239 34 1

So everything is correct, just assumption that 8-bit grayscale linearly converts to sRGB is wrong. 因此,一切都是正确的,仅假设8位灰度线性转换为sRGB是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM