Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

access bytes in BufferedImage.TYPE_BYTE_BINARY

4c3880db-960a-4cd4-9bc7-412ae46c86f2Apr 8 2017 — edited Apr 8 2017

am missing something obvious and was hoping someone could enlighten me ... i create a BufferedImage and i access the byte[] via ((DataBufferByte)inBuffImg.getRaster().getDataBuffer()).getData();

BufferedImage bimage = new BufferedImage(10,10,BufferedImage.TYPE_BYTE_GRAY);

gives me a byte[].length of 100 and i can access each individual pixel's gray value with & 0xFF

BufferedImage bimage = new BufferedImage(10,10,BufferedImage.TYPE_4BYTE_ABGR);

gives me a byte[].length of 400 and i can access each individual pixel's a b g and r values with & 0xFF ... all good so far ...

PROBLEM:

BufferedImage bimage = new BufferedImage(10,10,BufferedImage.TYPE_BYTE_BINARY);

gives me a byte[].length of 20 and i can't figure out how to access each individual pixel's 0,1 values from the byte array? (I know how to do this via getRGB() but this is too slow for my use here). any help greatly appreciated. thanks robert

update:

oh ... looks like padding causing the mismatch?

BufferedImage bimage = new BufferedImage(8,10,BufferedImage.TYPE_BYTE_BINARY);

gives byte[].length 10

that makes sense ... each pixel is now exactly a bit without padding because the width of the image is divisible by 8

BufferedImage bimage = new BufferedImage(9,10,BufferedImage.TYPE_BYTE_BINARY);

gives byte[].length 20

all the way up to

BufferedImage bimage = new BufferedImage(16,10,BufferedImage.TYPE_BYTE_BINARY);

gives byte[].length 20

i assume each new row of pixels starts with a bit at bit  zero. and end of each line padded to complete 8 bit boundary.

This post has been answered by 4c3880db-960a-4cd4-9bc7-412ae46c86f2 on Apr 8 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 6 2017
Added on Apr 8 2017
1 comment
776 views