Lesson 1. ByteImage


Lesson 2 : BitStreams | Contents


Start up wish version (8.0 or later), cd to the doc/tutorials directory, load the DvmBasic and DvmDisplay packages, and source the display library:  For example, assuming you unpacked Dali into C:/Dali, you would use the following commands.

cd C:/Dali/doc/tutorial
package require DvmBasic
package require DvmDisplay
source display.tcl

(Incidentally, all the commands in this lesson are stored in a file called l1.tcl in the same directory as display.tcl)

We'll first create a ByteImage so that we can try out a few simple commands. A ByteImage is a 2D array of bytes (i.e., a gray-scale image -- every pixel is a number between 0 and 255).

set a [byte_new 100 100]

This will create a new ByteImage of size 100 by 100. byte_new just allocates the ByteImage -- it doesn't initilized it. So lets initilalize it to 0 (all black) and see what it looks like:

byte_set $a 0
byte_display $a

You should see an image on the screen that  looks like Figure 1.


Figure 1

Now, lets draw a small gray box inside the square. We can do so by creating a virtual ByteImage (b) inside a.

set b [byte_clip $a 30 30 20 20]

This will create a small (20x20) ByteImage at position (30, 30) inside $a. Both a and b share the image memory, so modifying b will modify a as well. Let's initialize b to a gray color (pixel value = 128).

byte_set $b 128

Let's see what b looks like :

byte_display $b
byte_display $a

You should see a little gray box (Figure 2). If you display a, you will see a black box with a little gray box in it (figure 3).


Figure 2


Figure 3

Now lets make a duplicate of a. We can copy a into another ByteImage using byte_copy, but first we must allocate a new ByteImage (c).

set c [byte_new 100 100]
byte_copy $a $c

c will now be the exact copy of a. There are two pitfalls we should watch out for byte_copy. First, the source and the destination must not overlap, otherwise the result might be incorrect. Second, if the source and the destination is not of the same dimension, byte_copy will only copy an area of size w x h to the destination, where w and h are the minimum dimension of the two. For example, if we try to copy a 30 x 80 ByteImage into a 100 x 20 ByteImage, only the top left 30 x 20 bytes of the source will get copied.

Creating an image by setting the value is not very interesting. We will take a look at how we can initialize a ByteImage with data from a file, and output the content of the ByteImage to a file.

But before we continue, we should clean up the image that we've allocated so far.

byte_free $a

Before we continue to free b and c, it should be noted that accessing b after freeing a will lead to erronous result (possibly segmentation fault) since b borrows its memory from b.

byte_free $b
byte_free $c

Lesson 2 : BitStreams | Contents


Last Updated : 06/27/2025 02:27:15