Commit a04e1b8b authored by Dan Blezek's avatar Dan Blezek
Browse files

ADD: Added vtkImageDataToTkPhoto

parent d76676de
......@@ -21,6 +21,8 @@
#include <tcl.h>
#include <tk.h>
#include "vtkRenderWindowInteractor.h"
#include "vtkImageData.h"
#include "vtkTclUtil.h"
#ifdef _WIN32
#include "vtkWin32OpenGLRenderWindow.h"
......@@ -386,6 +388,10 @@ extern "C"
// vtkTkRenderWidget_Init
// Called upon system startup to create vtkTkRenderWidget command.
extern "C" {int VTK_TK_EXPORT Vtktkrenderwidget_Init(Tcl_Interp *interp);}
extern "C" {int VTK_TK_EXPORT vtkImageDataToTkPhoto_Cmd (ClientData clientData,
Tcl_Interp *interp,
int argc, char **argv); }
int VTK_TK_EXPORT Vtktkrenderwidget_Init(Tcl_Interp *interp)
{
if (Tcl_PkgProvide(interp,(char *)"Vtktkrenderwidget",(char *)"1.2") != TCL_OK)
......@@ -396,6 +402,9 @@ int VTK_TK_EXPORT Vtktkrenderwidget_Init(Tcl_Interp *interp)
Tcl_CreateCommand(interp, (char *) "vtkTkRenderWidget", vtkTkRenderWidget_Cmd,
Tk_MainWindow(interp), NULL);
Tcl_CreateCommand(interp, (char *) "vtkImageDataToTkPhoto", vtkImageDataToTkPhoto_Cmd,
NULL, NULL );
return TCL_OK;
}
......@@ -946,4 +955,112 @@ vtkTkRenderWidget_MakeRenderWindow(struct vtkTkRenderWidget *self)
return TCL_OK;
}
#endif
int vtkImageDataToTkPhoto_Cmd (ClientData clientData, Tcl_Interp *interp,
int argc, char **argv)
{
int status = 0;
vtkImageData* image = NULL;
Tk_PhotoHandle photo;
int z = 0;
// Usage: vtkImageDataToTkPhoto vtkImageData photo z
if ( argc != 4 )
{
Tcl_SetResult ( interp, "wrong # args: should be \"vtkImageDataToTkPhoto vtkImageData photo z\"", NULL );
return TCL_ERROR;
}
// Start with z, it's fast, etc...
status = Tcl_GetInt ( interp, argv[3], &z );
if ( status != TCL_OK )
{
return status;
}
// Find the image
#ifdef VTK_PYTHON_BUILD
int len = 1;
image = (vtkImageData*) vtkPythonUnmanglePointer ( argv[1], &len, "vtkImageData" );
char buffer[1024];
sprintf ( buffer, "Found: %d - %lx\n", len, (long)image );
Tcl_AppendResult ( interp, buffer, NULL );
if ( (void*)image == (void*)argv[1] ) { image = NULL; }
#else
image = (vtkImageData*) vtkTclGetPointerFromObject ( argv[1], "vtkImageData", interp, status );
#endif
if ( !image )
{
Tcl_AppendResult ( interp, "could not find vtkImageData: ", argv[1], NULL );
return TCL_ERROR;
}
// Find the photo widget
photo = Tk_FindPhoto ( interp, argv[2] );
if ( !photo )
{
Tcl_AppendResult ( interp, "could not find photo: ", argv[2], NULL );
return TCL_ERROR;
}
// Determine if we have the correct datatype
if ( image->GetScalarType() != VTK_UNSIGNED_CHAR )
{
Tcl_SetResult ( interp, "image data type must be of type unsigned char", NULL );
return TCL_ERROR;
}
int components = image->GetNumberOfScalarComponents();
if ( components != 1 && components != 3 )
{
Tcl_SetResult ( interp, "number of scalar components must be 1, 3, 4", NULL );
return TCL_ERROR;
}
int extent[6];
image->GetExtent ( extent );
if ( z < extent[4] || z > extent[5] )
{
Tcl_SetResult ( interp, "z is outside the image extent", NULL );
return TCL_ERROR;
}
extent[4] = extent[5] = z;
image->SetUpdateExtent ( extent );
image->Update();
// Setup the photo data block
Tk_PhotoImageBlock block;
block.pixelPtr = (unsigned char*) image->GetScalarPointerForExtent ( extent );
// Read upwards from the bottom of the image
block.pixelPtr = (unsigned char*) image->GetScalarPointer ( 0, extent[3], z );
block.width = extent[1] - extent[0] + 1;
block.height = extent[3] - extent[2] + 1;
block.pitch = -components * ( block.width );
block.pixelSize = components;
block.offset[0] = 0;
block.offset[1] = 1;
block.offset[2] = 2;
block.offset[3] = 0;
switch ( components )
{
case 1:
block.offset[0] = 0;
block.offset[1] = 0;
block.offset[2] = 0;
block.offset[3] = 0;
break;
case 3:
block.offset[3] = 0;
break;
case 4:
block.offset[3] = 3;
break;
}
Tk_PhotoSetSize ( photo, block.width, block.height );
Tk_PhotoPutBlock ( photo, &block, 0, 0, block.width, block.height );
return TCL_OK;
}
#endif
......@@ -17,4 +17,5 @@
=========================================================================*/
#define VTK_PYTHON_BUILD
#include "vtkPythonUtil.h"
#include "vtkTkRenderWidget.cxx"
......@@ -20,6 +20,7 @@
#include "vtkTkImageViewerWidget.h"
#include "vtkTkRenderWidget.h"
#include "vtkImageData.h"
//----------------------------------------------------------------------------
// Vtkrenderingpythontkwidgets_Init
......@@ -32,6 +33,9 @@ extern "C"
int argc, char **argv);
int vtkTkImageViewerWidget_Cmd(ClientData clientData, Tcl_Interp *interp,
int argc, char **argv);
int vtkImageDataToTkPhoto_Cmd (ClientData clientData,
Tcl_Interp *interp,
int argc, char **argv);
}
int Vtkrenderingpythontkwidgets_Init(Tcl_Interp *interp)
......@@ -46,5 +50,8 @@ int Vtkrenderingpythontkwidgets_Init(Tcl_Interp *interp)
Tcl_CreateCommand(interp, (char *) "vtkTkImageViewerWidget",
vtkTkImageViewerWidget_Cmd, Tk_MainWindow(interp), NULL);
Tcl_CreateCommand(interp, (char *) "vtkImageDataToTkPhoto", vtkImageDataToTkPhoto_Cmd,
NULL, NULL );
return TCL_OK;
}
"""Tkinter widgets for VTK."""
__all__ = ['vtkTkRenderWidget', 'vtkTkImageViewerWidget',
'vtkTkRenderWindowInteractor']
'vtkTkRenderWindowInteractor', 'vtkTkPhotoImage']
"""
A subclass of Tkinter.PhotoImage that connects a
vtkImageData to a photo widget.
Created by Daniel Blezek, August 2002
"""
import Tkinter
from Tkinter import *
from vtkpython import *
from vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgets
class vtkTkPhotoImage ( Tkinter.PhotoImage ):
"""
A subclass of PhotoImage with helper functions
for displaying vtkImageData
"""
def __init__ ( self, **kw ):
# Caller the superclass
Tkinter.PhotoImage.__init__ ( self, kw )
vtkLoadPythonTkWidgets ( self.tk )
def PutImageSlice ( self, image, z ):
t = cast.GetOutput().__this__
photo.tk.eval ( 'vtkImageDataToTkPhoto ' + t[:-2] + ' ' + self.name + ' ' + z );
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment