2

我有一个放大图像的 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...

推荐的方法是什么?

4

1 回答 1

1

您可以使用 Tcl_ByteArraySetLength 或 Tcl_GetByteArrayFromObj 将 Tcl ByteArrayObj 类型视为缓冲区,这两者都允许您访问 Tcl 对象的数据部分。

Tcl_Obj *dataObj = Tcl_NewObj();
char *dataPtr = Tcl_SetByteArrayLength(dataObj, 1024);

现在您可以使用 dataPtr 来设置对象中的字节。Tcl_SetByteArrayLength 函数将使这个对象成为一个 ByteArray 类型。

但是,您可能还想查看我用于拉伸 Tk 图像的imgscale ,它使用各种插值模式,例如:

image create photo thumbnail
::imgscale::bilinear $myphoto 64 64 thumbnail 1.0

将某些照片缩小为缩略图。

于 2012-12-06T12:23:36.613 回答