c++ - 如何将 C++ unsigned char [] 传递给 Tcl bytearray?

标签 c++ tcl

我有一个放大图像的 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”是一个 ByteArray。这是正确的吗?

相关代码片段如下所示:

// ...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...

推荐的做法是什么?

最佳答案

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

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

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

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

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

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

关于c++ - 如何将 C++ unsigned char [] 传递给 Tcl bytearray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13743035/

相关文章:

c++ - 为什么 json 文件从 C++ 中删除旧输入?

c++ - 从 concurrent_unordered_map 中删除项目列表

c++ - 使用 C++ 获取 Linux 中进程的 RAM 和 CPU 使用情况

sqlite - 在 Tcl 列表中将元素用单引号括起来以进行 sqlite 插入

c++ - 这是列表初始化还是值初始化?

c++ - 在模板化类中重载 << 运算符会给我链接器错误 (C++)

xml - 在TCL tDOM中解析具有多个子项的XML数据

namespaces - Tcl 命名空间重命名以避免冲突

Tcl 开发工具包 : can't find package starkit

recursion - Tcl - 递归遍历和FTP上传