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

}

このTclプロシージャをC++から呼び出したいと思います。

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

これを行うための推奨される方法は何ですか?

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 に答える