开发者

using Buffered Image to write RGB images

开发者 https://www.devze.com 2023-02-14 10:31 出处:网络
I have a 2 dimensionalbyte array I[][], each entry in which is either 0 or -1 (i.e 255) Now I am using the following code to write the image in RGB format.

I have a 2 dimensional byte array I[][], each entry in which is either 0 or -1 (i.e 255)

Now I am using the following code to write the image in RGB format.

    public void writeImage(Str开发者_高级运维ing outputFileName){
    BufferedImage BI = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for(int y = 0;y < height;y++){
        for(int x = 0;x < width;x++){
            int gray = (int) I[y][x] & 0xFF;
            int newRGB = (gray << 16) + (gray << 8) + gray;
            BI.setRGB(x, y, newRGB);
        }
    }
    try{
        ImageIO.write(BI, "JPG", new File(outputFileName));
    } catch(IOException e){
        System.err.println("Unable to output results");
    }
}

The image is written succesfully. Since the original data array contained only two different values (0 and 255), i expected that the written image will also have two distinct levels. However the written image has 92 different levels. What am I doing wrong ?


You're not doing anything wrong, but JPEG compression means that the levels in each pixel are not exactly recorded -- when the image is uncompressed the JPEG compression artifacts spread sharp edges slightly to other pixels.

0

精彩评论

暂无评论...
验证码 换一张
取 消