繁体   English   中英

如何将C ++无符号char []传递给Tcl字节数组?

[英]How to pass C++ unsigned char [] to to Tcl bytearray?

我有一个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类型。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM