The moose likes Java in General and the fly likes reading in files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "reading in files" Watch "reading in files" New topic

reading in files

CPT Finch

Joined: Jul 12, 2006
Posts: 4
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

Joined: Jul 08, 2003
Posts: 24199


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.

[Jess in Action][AskingGoodQuestions]
CPT Finch

Joined: Jul 12, 2006
Posts: 4
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
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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.

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
CPT Finch

Joined: Jul 12, 2006
Posts: 4
it's great to know the API has that - I hate to reinvent wheels!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15084

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: reading in files
It's not a secret anymore!