Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.8K Databases
- 221.5K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.8K SQL & PL/SQL
- 21.3K SQL Developer
- 295.4K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 27 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 393 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
access bytes in BufferedImage.TYPE_BYTE_BINARY

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.
Best Answer
-
looks like this strategy might get me there ... first need to know the number of bytes per row for the inBuffImg with ...
int bytesPerRow = inBuffImg.getWidth()/8;
if (inBuffImg.getWidth()%8>0)
bytesPerRow+=1;
then i get bit and byte positions with
int bytePos = y*bytesPerRow + x/8;
int bitPos = x%8;
then i get the bit value
pixels = ((DataBufferByte)inBuffImg.getRaster().getDataBuffer()).getData();
return (pixels[bytePos] >> bitPos) & 1;
Answers
-
looks like this strategy might get me there ... first need to know the number of bytes per row for the inBuffImg with ...
int bytesPerRow = inBuffImg.getWidth()/8;
if (inBuffImg.getWidth()%8>0)
bytesPerRow+=1;
then i get bit and byte positions with
int bytePos = y*bytesPerRow + x/8;
int bitPos = x%8;
then i get the bit value
pixels = ((DataBufferByte)inBuffImg.getRaster().getDataBuffer()).getData();
return (pixels[bytePos] >> bitPos) & 1;