views:

101

answers:

2
+1  Q: 

Monochrome Bitmap

Should be an easy one.

I'm working on Scala trying to handle long sequences of binary data. That is long lists of 0's and 1's. What is the 'best' way to store/access this kind of data.

The important point here is memory optimisation, so I would like to avoid using an entire byte to store a boolean. Also access is somwhat important, so I would like to avoid paking them into bytes and then into arrays.

Is a BitMap a good idea? Is there such a class in scala?

if not, would it be best to use ByteArray? How would you implement this?

Any other ideas?

Thanks,

+2  A: 

You can use java.util.BitSet (perhaps with a couple if clever explicits to make it more Scala-like).

If that is still too costly I would write a class that uses an array internally and pack the bits into ints or bytes.

leonm
Great! I found Scala has a BitSet too (http://www.scala-lang.org/docu/files/api/scala/collection/BitSet.html). The only problem is the size might be too small (indicated by Int).Thanks,
Skuge
+2  A: 

If your values are not uniformly distributed have (significantly more 0s than 1s) you can use run-length encoding to encode the image data. This is the encoding used by Fax.

There are two encoding options:

  • use RLE for black and white
  • use only RLE for one color and use a direct encoding if you encode the other color (or mixed sections)
Thomas Jung
Great idea, but I'm looking for the data structure were I can store this information. If you do run-length encoding, would you use a list of Int? Since ints are 32 bit long, seems like you need very special distributions to get a good compression (like you say, a piece of paper is a good example of big white spaces)
Skuge
Int is probably wasted if you take a Byte you can encode a length of 2^7 saving 1 bit for black or white colors.
Thomas Jung