I have a 192x192 element array of booleans, and I need to store it to a file. I want to pack each 8 bits into one byte for speed and file size reasons. I have something that sort of works but I don't think its doing it quite right....
VB Code:
Dim I, J as Integer Dim NextByte As Byte Dim Writer As New IO.StreamWriter(IO.File.Create("c:\asdf\" & Letter & ".dat"), System.Text.Encoding.ASCII) For I = 0 To 191 For J = 0 To 23 If BitBoard(J * 8, I) = True Then NextByte = NextByte Or 128 'flip bit 1 If BitBoard(J * 8 + 1, I) = True Then NextByte = NextByte Or 64 'flip bit 2 If BitBoard(J * 8 + 2, I) = True Then NextByte = NextByte Or 32 'flip bit 3 If BitBoard(J * 8 + 3, I) = True Then NextByte = NextByte Or 16 'flip bit 4 If BitBoard(J * 8 + 4, I) = True Then NextByte = NextByte Or 8 'flip bit 5 If BitBoard(J * 8 + 5, I) = True Then NextByte = NextByte Or 4 'flip bit 6 If BitBoard(J * 8 + 6, I) = True Then NextByte = NextByte Or 2 'flip bit 7 If BitBoard(J * 8 + 7, I) = True Then NextByte = NextByte Or 1 'flip bit 8 Writer.Write(Chr(NextByte)) NextByte = NextByte AndAlso 0 Next Next Writer.Close()
And to unpack it....
SampleImage.SetPixel to flip make a pixel black....this is 1 bit color image storage.VB Code:
Dim i, J As Integer Dim NextChar As Byte Dim reader As New IO.StreamReader(IO.File.Open("c:\asdf\hello.dat", IO.FileMode.Open)) For i = 0 To 191 For J = 0 To 23 NextChar = reader.Read() If NextChar And 128 Then SampleImage.SetPixel(J * 8, i, Color.Black) If NextChar And 64 Then SampleImage.SetPixel(J * 8 + 1, i, Color.Black) If NextChar And 32 Then SampleImage.SetPixel(J * 8 + 2, i, Color.Black) If NextChar And 16 Then SampleImage.SetPixel(J * 8 + 3, i, Color.Black) If NextChar And 8 Then SampleImage.SetPixel(J * 8 + 4, i, Color.Black) If NextChar And 4 Then SampleImage.SetPixel(J * 8 + 5, i, Color.Black) If NextChar And 2 Then SampleImage.SetPixel(J * 8 + 6, i, Color.Black) If NextChar And 1 Then SampleImage.SetPixel(J * 8 + 7, i, Color.Black) Next NextChar = NextChar AndAlso 0 Next
It sort of works but I get weird results...I think its something going on wiht my boolean logic (or lack thereof)
Any ideas?
TIA




)
Reply With Quote