How do I read/write binary image data? (fwd)
- To: mathgroup at smc.vnet.net
- Subject: [mg7932] How do I read/write binary image data? (fwd)
- From: tjwilli at dc.net (Tim Williams)
- Date: Wed, 23 Jul 1997 15:46:10 -0400
- Organization: Posted via CAIS Internet <info@cais.com>
- Sender: owner-wri-mathgroup at wolfram.com
(I'm posting this from my home ISP since our news poster at work seems to be flaky with moderated newsgroups. Please reply to williams at nvl.army.mil. Thanks.) I'm posting this for someone esle who is trying to use Mathematica 3.0 on a Pentium 166. We'd like to be able to read/write binary image data into Mathematica to do operations on the image. The input data is a 256x256 2-byte (signed short) image which can be read in w/o too much trouble with something like: file1="C:\PICTURE"; OpenRead[file1]; SetStreamPosition[file1,0]; data1=ReadListBinary[file1,Table[Int16,{256}],256,RecordLists->True]; data1=Reverse[data1]; ListDensityPlot[data1,Mesh->False,PlotRange->{Min[data1],Max[data1]}]; data1 is Reversed so the display isn't upside down. The output is a 256x256 float image. We'd like to write this data out in raw binary format for transfer to another platform. We've tried stream=OpenWriteBinary["A:\mydata"]; WriteBinary[stream,data]; Close["A:\mydata"]; but gave up waiting for it to do anything. (Reading the input only took about 2 minutes. Long, but reasonable.) Writing one line at a time with stream=OpenWriteBinary["A:\mydata"]; WriteBinary[stream,data[[1]]]; Close["A:\mydata"]; For[i=2,i<257,i++,stream=OpenAppendBinary["A:\mydata"]; WriteBinary[stream,data[[i]]];Close["A:\mydata"];]; took about an hour. This is ridiculous. A C program could do this in seconds. Isn't there a better way? Please e-mail responses. Thanks in advance. Tim Williams williams at nvl.army.mil