Lesson 2. BitStreams


Lesson 1 : ByteImage | Lesson 3 : BitParser | Contents


This lesson discusses how Input/Output is handled in Dali. It assumes you are runnign wish and have loaded the DvmBasic package.  Start up wish version (8.0 or later), cd to the doc/tutorials directory, and load the DvmBasic.  For example, assuming you unpacked Dali into C:/Dali, you would use the following commands.

cd C:/Dali/doc/tutorial
package require DvmBasic

Input/output in Dali are done through a BitStream, which is part of the DvmBasic package.  A BitStream is essentially an I/O buffer.  We start by creating a BitStream by calling bitstream_new. We need to tell Dali how big we want our BitStream to be.

set bs [bitstream_new 300]

This will create a BitStream bs that can hold 300 bytes. bs is not initialized at this moment, we can initialized the BitStream by filling it with bits from a Tcl channel. So let's create a Tcl channel from a file call lenna.ppm, and fill up the BitStream with it.

set f [open lenna.ppm r]
bitstream_channel_read $bs $f 0

The last command will return the number of bytes read into bs, which should be 300.

The last argument to bitstream_channel_read specifies the offset in bs where we should start filling the BitStream. Let's try changing the last argument to 100

bitstream_channel_read $bs $f 100

This command initializes bytes 100 - 299 with the next 200 bytes from lenna.pgm and returns 200.

If we only want toread, say, 50 bytes into offset 100, we use bitstream_channel_read_segment instead of bitstream_channel_read. bitstream_channel_read_segment takes in an extra parameter that specify the number of bytes to read:

bitstream_channel_read_segment $bs $f 100 50

This function reads the next 50 bytes from lenna.pgm into bs.

We now take a look at how we can output the content of the BitStream to a Tcl channel. But before we do so, we should explain the meaning of "valid" data in a BitStream.  Intuitively, "valid" data are bytes that are written into the BitStream, but not yet written to the output. A newly create bitstream has no valid data. After we call

bitstream_channel_read_segment $bs $f 100 50

The BitStream has 50 bytes of valid data -- bytes 100 to 149.

We can write contents of the bitstream to the file bar.tst as follows:

set g [open bar.tst w]
bitstream_channel_write $bs $g 0

This will write data into bar.tst, starting from byte 0 of bs up through the last byte of valid data. It is incorrect to say that this output only "valid" data to the channel, because if we call :

set bs [bitstream_new 300]
bitstream_channel_read_segment $bs $f 100 50
bitstream_channel_write $bs $g 0
close $g

We will be writing 150 bytes of data from bs to bar.tst, but the first 100 bytes is not initialized and therefore is not valid. After writing all bytes in the BitStream is considered invalid again.

Next, we will take a look at how we can read and write data from a BitStream.


Lesson 1 : ByteImage | Lesson 3 : BitParser | Contents


Last Updated : 06/04/2025 02:36:41