我有一个放大图像的 Tcl 程序。
proc ShowWindow {wtitle zfactor imgdata} {
puts stdout "Now in the Tcl procedure ShowWindow!";
image create photo idata -data $imgdata; # create a photo image from the input data
image create photo the_window; # final window
the_window copy idata -zoom $zfactor $zfactor; # copy the original and magnify
wm title . $wtitle; # window title
wm resizable . false false; # no resizing in both x and y directions
catch {destroy .dwindow}; # since this procedure will be called multiple times
# we need to suppress the 'window name "dwindow" already exists in parent' message
label .dwindow -image the_window; # create a label to display the image
pack .dwindow; # display the image
}
我想从 C++ 调用这个 Tcl 过程。
我认为“imgdata”是一个字节数组。这个对吗?
相关代码片段如下所示:
// ...Tcl/Tk initialization omitted...
unsigned char *img; // PPM image data
int num_bytes; // PPM image file size
// ...routines to initialize img and num_bytes omitted...
Tcl_Obj *tcl_raw;
// transfer the PPM image data into Tcl_Obj
if (!(tcl_raw = Tcl_NewByteArrayObj(img, num_bytes))) {
cerr << "Tcl_NewByteArrayObj() failed!" << endl;
cerr << "Exiting..." << endl;
return 1;
} else {
cerr << "Tcl_NewByteArrayObj() succeeded!" << endl;
}
Tcl_IncrRefCount(tcl_raw); // is this really necessary?
// set the Tcl variable "imgdata" to the Tcl_Obj
if (Tcl_SetVar2Ex(tcl_interpreter, "imgdata", "", tcl_raw, TCL_LEAVE_ERR_MSG) == NULL) {
cerr << "Tcl_SetVar2Ex() failed!" << endl;
cerr << "Exiting..." << endl;
return 1;
} else {
cerr << "Tcl_SetVar2Ex() succeeded!" << endl;
}
// execute the Tcl procedure
if (Tcl_Eval(tcl_interpreter, "ShowWindow TheImage 8 $imgdata") != TCL_OK) {
cerr << "Tcl_Eval() failed!" << endl;
cerr << "Tcl_GetStringResult() = " << Tcl_GetStringResult(tcl_interpreter) << endl;
cerr << "Exiting..." << endl;
return 1;
} else {
cerr << "Tcl_Eval() succeeded!" << endl;
}
程序在 Tcl_Eval() 处失败。程序输出为:
...
Tcl_NewByteArrayObj() succeeded!
Tcl_SetVar2Ex() succeeded!
Tcl_Eval() failed!
Tcl_GetStringResult() = can't read "imgdata": variable is array
Exiting...
推荐的方法是什么?