• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading in files

 
CPT Finch
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a region grow program that will count the number of connected 'on' pixels to give me the size of a region.

To do this I need to read in a binary bitmap file and get access to the on off pixels.

Does anyone have any idea how to read in a binary bitmap file?

or additionally any tips on how to go about writing this?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

I'd just read the file into a byte[] using a FileInputStream, and write a few routines to compute the location of the bit corresponding to given x, y coordinates. You'd have to compute which byte the bit would be in, then what position within that byte, and then use the bitwise "&" operator to test its value.
 
CPT Finch
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou,
I agree that would be a good way once getting to the image data but there are headers with bitmap files. How does one deal with these - is there a bitmap file reader?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is now a default BMP decoder in the Java Image I/O API. I nearly wrote one myself, but that was a long time ago now - before they added it to the standard API! Having said that, I seem to remember that the bitmap format is pretty self-explanatory anyway. If you were to write your own decoder, probably the biggest problem you'd face is dealing with the bit format because (if I remember correctly), Bitmap is little-endian whereas Java is big-endian. So you'd need to flip all the bytes around - but NIO will do that for you. Basically, once you've figured out how to skip the headers (which wouldn't be too hard), the rest of the data is just the bits which make up the image so you can extract them directly.

Anyway, you can make use of the ImageIO API (javax.imageio) which can work with JPEG, PNG, BMP and GIF as standard.
 
CPT Finch
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
it's great to know the API has that - I hate to reinvent wheels!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charles Lyons:
... Bitmap is little-endian whereas Java is big-endian. So you'd need to flip all the bytes around...

Java is big-endian?! I've never heard about that. Java is not endian at all. The endianness depends on the platform it's running on. You don't know (and you don't need to know) how the JVM stores an int, long or other data type in memory exactly.

What made you think that Java is big-endian and can you show a piece of code that demonstrates this?

In Windows BMP files, pixel values are usually stored in the order B, G, R (not R, G, B), but that doesn't really have anything to do with endianness.
[ July 18, 2006: Message edited by: Jesper Young ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic