MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: binary import

  • To: mathgroup at
  • Subject: [mg25990] Re: binary import
  • From: "Mariusz Jankowski" <mjkcc at>
  • Date: Wed, 15 Nov 2000 02:09:41 -0500 (EST)
  • Organization: University of Southern Maine
  • References: <8ug936$>
  • Sender: owner-wri-mathgroup at


The fastest method of reading binary data is ReadList. In version4 use the
following command:

strm = OpenRead["filename", DOSTextFormat->True]; (* the option is needed in
Windows only *)

data = Developer`ToPackedArray[ReadList[strm, Byte]];


The new packed storage format uses 4 bytes per integer, so the ByteCount for
data is 4 * Length[data].
Next convert the 1-byte per sample data to 2-bytes per sample (assuming LSB
is first) by combining pairs of integers in data:

data16bit = ({1, 256} . # &) /@ Partition[data,2]

This operation is also reasonably fast. Of course you must also Partition
data16bit, so that you obtain a 2D array. For that you must know the column
dimension of your images.


Mariusz Jankowski
University of Southern Maine
mjkcc at

"Bruyndonckx P." <pbruynd at> wrote in message
news:8ug936$id7 at
> I would like to use mathemtica to do some image processing.  I have 2D
images stored in a file as binary 16 bit data.  When I
> try to use the binaryimport, I seem to have two problems :
> 1) to read 100,000 binary 16 bit numbers, it takes about 255 seconds, only
392 numbers/s.  Why is this so terribly slow ???
> 2) When I compare the memory in use (MemoryInUse[] command) before and
after I use the BinaryImport command, it seems that I
> need about 892,000 bytes to store these 100,000 number, ie nearly 9 bytes
per 16 bit number.  Why is MAthematica so
> inefficient in storing a list of binary numbers ?  Or does it use memory
for some other purpose.
> When I used the command ReadListBinary it takes 320  seconds to read
100,000 16 bit numbers.
> Does anybody have a workable solution, memory wise as well as time wise ?
> Thanks

  • Prev by Date: Re: Two problems with Mathematica 4 on G4
  • Next by Date: Re: possible bug in Mathematica?
  • Previous by thread: binary import
  • Next by thread: LeafCount