# Copyright (C) 2002-2003 Tesseract Technology
#
# You can cut and paste this into a file, then run it like this:
# vtk <nameOfSavedScriptFile>
#
# when you run in this way, effectively you are running the 'vtk' tcl/tk
# 'wish' wrapper. If you were to just run 'vtk' you would be able to type
# in straight tcl and tk, as well as evaluate vtk expression.
#
# this gets the VTK package loaded under the quasi-wishx;
# vtkinteraction is useful for debugging and interacting with VTK widgets
# at runtime, in addition to other handy features
package require vtk
package require vtkinteraction
set x 256
set y 256
set limit [ expr $x * $y ]
# create an array of integers
# when you set the number of values, allocation will take place automatically
vtkIntArray iArr
iArr SetNumberOfValues [expr $x * $y ]
# load some test data into buffer; basically scale from 0 to length of
# array, store 4*index into indexed element
for { set i 0 } { $i < $limit } { incr i 1 } {
iArr SetValue $i [ expr $i * 4 ]
}
# vtkImageData is derived from vtkDataSet
# vtkDataSets need to know about what their organization is
# this data set is:
# X by Y by N of planes of data
# has a scalar type of integer
# spacing is the unit sampling interval between points
# (in this case, 1.0, 1 sample/pixel, in X,Y, and Z dimensions)
# origin is World Coordinate Position of lower left hand corner of dataset
vtkImageData imgData
imgData SetDimensions $x $y 1
imgData SetScalarTypeToInt
imgData SetSpacing 1.0 1.0 1.0
imgData SetOrigin 0.0 0.0 0.0
# unlike a vtkIntArray, vtkImageData needs to be explicitly allocated,
# at least for the purposes of this example
imgData AllocateScalars
# finally, the image data should assign it's scalar values from
# the integer array, which is already loaded
[imgData GetPointData] SetScalars iArr
# vtkImageViewer is a convenience wrapper around
# vtkImageWindow, vtkImager, vtkActor2D and vtkImageMapper
# also, this is a suitable argument to Tk image viewer widget
vtkImageViewer imgViewer
# set image viewer widget's input to image data created below
# note that you don't eval output (GetOutput()) of image data,
# you just pass it as arg to SetInput of image viewer; this
# tends to be confusing because in pretty much all other cases
# the semantics of SetInput are taken as result of GetOutput
imgViewer SetInput imgData
# The color window and level model of LUT translation is quite flexible
# but has to be mapped onto the concepts of contrast and brightness.
# The 'window' is the width of the input interval. For example, if you
# have 256 values of input, then the input window would be 256 in order
# to map all possible input values.
# The 'level' is the center (median) value of the input. Again, if you wanted
# to view data from 0 to 256, halfway would be 128, and the window would
# be 256 wide.
# Input values below the resulting input window are mapped to the lowest
# possible output value; input values above the input window are mapped to
# the highest possible output value.
#
imgViewer SetColorWindow 262144
imgViewer SetColorLevel [ expr 262144/2 ]
# finally, the vtkTkImageViewerWidget is a tk-happy widget that contains
# several other classes used to build a quick-n-easy image display window.
# Notice that, although the return value of the object creation is later
# passed to a function, it is the name of the widget in the tk display
# hierarchy that is used for pack/grid purposes under tk.
set vtkiw [vtkTkImageViewerWidget .view \
-width $x \
-height $y \
-iv imgViewer ]
# without this, it won't view....
::vtk::bind_tk_imageviewer_widget $vtkiw
# finally, by packing the widget, you will see it....
pack .view
Here is the resulting image as displayed on my 24-bit display:
Back to Tesseract Tech's VTK page