| 1 |
/* |
| 2 |
* bltWinop.c -- |
| 3 |
* |
| 4 |
* This module implements simple window commands for the BLT toolkit. |
| 5 |
* |
| 6 |
* Copyright 1991-1998 Lucent Technologies, Inc. |
| 7 |
* |
| 8 |
* Permission to use, copy, modify, and distribute this software and |
| 9 |
* its documentation for any purpose and without fee is hereby |
| 10 |
* granted, provided that the above copyright notice appear in all |
| 11 |
* copies and that both that the copyright notice and warranty |
| 12 |
* disclaimer appear in supporting documentation, and that the names |
| 13 |
* of Lucent Technologies any of their entities not be used in |
| 14 |
* advertising or publicity pertaining to distribution of the software |
| 15 |
* without specific, written prior permission. |
| 16 |
* |
| 17 |
* Lucent Technologies disclaims all warranties with regard to this |
| 18 |
* software, including all implied warranties of merchantability and |
| 19 |
* fitness. In no event shall Lucent Technologies be liable for any |
| 20 |
* special, indirect or consequential damages or any damages |
| 21 |
* whatsoever resulting from loss of use, data or profits, whether in |
| 22 |
* an action of contract, negligence or other tortuous action, arising |
| 23 |
* out of or in connection with the use or performance of this |
| 24 |
* software. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "bltInt.h" |
| 28 |
|
| 29 |
#ifndef NO_WINOP |
| 30 |
|
| 31 |
#include "bltImage.h" |
| 32 |
#include <X11/Xutil.h> |
| 33 |
#ifndef WIN32 |
| 34 |
#include <X11/Xproto.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
static Tcl_CmdProc WinopCmd; |
| 38 |
|
| 39 |
static int |
| 40 |
GetRealizedWindow(interp, string, tkwinPtr) |
| 41 |
Tcl_Interp *interp; |
| 42 |
char *string; |
| 43 |
Tk_Window *tkwinPtr; |
| 44 |
{ |
| 45 |
Tk_Window tkwin; |
| 46 |
|
| 47 |
tkwin = Tk_NameToWindow(interp, string, Tk_MainWindow(interp)); |
| 48 |
if (tkwin == NULL) { |
| 49 |
return TCL_ERROR; |
| 50 |
} |
| 51 |
if (Tk_WindowId(tkwin) == None) { |
| 52 |
Tk_MakeWindowExist(tkwin); |
| 53 |
} |
| 54 |
*tkwinPtr = tkwin; |
| 55 |
return TCL_OK; |
| 56 |
} |
| 57 |
|
| 58 |
static Window |
| 59 |
StringToWindow(interp, string) |
| 60 |
Tcl_Interp *interp; |
| 61 |
char *string; |
| 62 |
{ |
| 63 |
int xid; |
| 64 |
|
| 65 |
if (string[0] == '.') { |
| 66 |
Tk_Window tkwin; |
| 67 |
|
| 68 |
if (GetRealizedWindow(interp, string, &tkwin) != TCL_OK) { |
| 69 |
return None; |
| 70 |
} |
| 71 |
if (Tk_IsTopLevel(tkwin)) { |
| 72 |
return Blt_GetRealWindowId(tkwin); |
| 73 |
} else { |
| 74 |
return Tk_WindowId(tkwin); |
| 75 |
} |
| 76 |
} else if (Tcl_GetInt(interp, string, &xid) == TCL_OK) { |
| 77 |
#ifdef WIN32 |
| 78 |
static TkWinWindow tkWinWindow; |
| 79 |
|
| 80 |
tkWinWindow.handle = (HWND)xid; |
| 81 |
tkWinWindow.winPtr = NULL; |
| 82 |
tkWinWindow.type = TWD_WINDOW; |
| 83 |
return (Window)&tkWinWindow; |
| 84 |
#else |
| 85 |
return (Window)xid; |
| 86 |
#endif |
| 87 |
} |
| 88 |
return None; |
| 89 |
} |
| 90 |
|
| 91 |
#ifdef WIN32 |
| 92 |
|
| 93 |
static int |
| 94 |
GetWindowSize( |
| 95 |
Tcl_Interp *interp, |
| 96 |
Window window, |
| 97 |
int *widthPtr, |
| 98 |
int *heightPtr) |
| 99 |
{ |
| 100 |
int result; |
| 101 |
RECT region; |
| 102 |
TkWinWindow *winPtr = (TkWinWindow *)window; |
| 103 |
|
| 104 |
result = GetWindowRect(winPtr->handle, ®ion); |
| 105 |
if (result) { |
| 106 |
*widthPtr = region.right - region.left; |
| 107 |
*heightPtr = region.bottom - region.top; |
| 108 |
return TCL_OK; |
| 109 |
} |
| 110 |
return TCL_ERROR; |
| 111 |
} |
| 112 |
|
| 113 |
#else |
| 114 |
|
| 115 |
/* |
| 116 |
*---------------------------------------------------------------------- |
| 117 |
* |
| 118 |
* XGeometryErrorProc -- |
| 119 |
* |
| 120 |
* Flags errors generated from XGetGeometry calls to the X server. |
| 121 |
* |
| 122 |
* Results: |
| 123 |
* Always returns 0. |
| 124 |
* |
| 125 |
* Side Effects: |
| 126 |
* Sets a flag, indicating an error occurred. |
| 127 |
* |
| 128 |
*---------------------------------------------------------------------- |
| 129 |
*/ |
| 130 |
/* ARGSUSED */ |
| 131 |
static int |
| 132 |
XGeometryErrorProc(clientData, errEventPtr) |
| 133 |
ClientData clientData; |
| 134 |
XErrorEvent *errEventPtr; |
| 135 |
{ |
| 136 |
int *errorPtr = clientData; |
| 137 |
|
| 138 |
*errorPtr = TCL_ERROR; |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
static int |
| 143 |
GetWindowSize(interp, window, widthPtr, heightPtr) |
| 144 |
Tcl_Interp *interp; |
| 145 |
Window window; |
| 146 |
int *widthPtr, *heightPtr; |
| 147 |
{ |
| 148 |
int result; |
| 149 |
int any = -1; |
| 150 |
int x, y, borderWidth, depth; |
| 151 |
Window root; |
| 152 |
Tk_ErrorHandler handler; |
| 153 |
Tk_Window tkwin; |
| 154 |
|
| 155 |
tkwin = Tk_MainWindow(interp); |
| 156 |
handler = Tk_CreateErrorHandler(Tk_Display(tkwin), any, X_GetGeometry, |
| 157 |
any, XGeometryErrorProc, &result); |
| 158 |
result = XGetGeometry(Tk_Display(tkwin), window, &root, &x, &y, |
| 159 |
(unsigned int *)widthPtr, (unsigned int *)heightPtr, |
| 160 |
(unsigned int *)&borderWidth, (unsigned int *)&depth); |
| 161 |
Tk_DeleteErrorHandler(handler); |
| 162 |
XSync(Tk_Display(tkwin), False); |
| 163 |
if (result) { |
| 164 |
return TCL_OK; |
| 165 |
} |
| 166 |
return TCL_ERROR; |
| 167 |
} |
| 168 |
#endif /*WIN32*/ |
| 169 |
|
| 170 |
|
| 171 |
#ifndef WIN32 |
| 172 |
/*ARGSUSED*/ |
| 173 |
static int |
| 174 |
ColormapOp(clientData, interp, argc, argv) |
| 175 |
ClientData clientData; |
| 176 |
Tcl_Interp *interp; |
| 177 |
int argc; /* Not used. */ |
| 178 |
char **argv; |
| 179 |
{ |
| 180 |
register int i; |
| 181 |
Tk_Window tkwin; |
| 182 |
#define MAXCOLORS 256 |
| 183 |
register XColor *colorPtr; |
| 184 |
XColor colorArr[MAXCOLORS]; |
| 185 |
unsigned long int pixelValues[MAXCOLORS]; |
| 186 |
int inUse[MAXCOLORS]; |
| 187 |
char string[20]; |
| 188 |
unsigned long int *indexPtr; |
| 189 |
int nFree; |
| 190 |
|
| 191 |
if (GetRealizedWindow(interp, argv[2], &tkwin) != TCL_OK) { |
| 192 |
return TCL_ERROR; |
| 193 |
} |
| 194 |
/* Initially, we assume all color cells are allocated. */ |
| 195 |
memset((char *)inUse, 0, sizeof(int) * MAXCOLORS); |
| 196 |
|
| 197 |
/* |
| 198 |
* Start allocating color cells. This will tell us which color cells |
| 199 |
* haven't already been allocated in the colormap. We'll release the |
| 200 |
* cells as soon as we find out how many there are. |
| 201 |
*/ |
| 202 |
nFree = 0; |
| 203 |
for (indexPtr = pixelValues, i = 0; i < MAXCOLORS; i++, indexPtr++) { |
| 204 |
if (!XAllocColorCells(Tk_Display(tkwin), Tk_Colormap(tkwin), |
| 205 |
False, NULL, 0, indexPtr, 1)) { |
| 206 |
break; |
| 207 |
} |
| 208 |
inUse[*indexPtr] = TRUE;/* Indicate the cell is unallocated */ |
| 209 |
nFree++; |
| 210 |
} |
| 211 |
XFreeColors(Tk_Display(tkwin), Tk_Colormap(tkwin), pixelValues, nFree, 0); |
| 212 |
for (colorPtr = colorArr, i = 0; i < MAXCOLORS; i++, colorPtr++) { |
| 213 |
colorPtr->pixel = i; |
| 214 |
} |
| 215 |
XQueryColors(Tk_Display(tkwin), Tk_Colormap(tkwin), colorArr, MAXCOLORS); |
| 216 |
for (colorPtr = colorArr, i = 0; i < MAXCOLORS; i++, colorPtr++) { |
| 217 |
if (!inUse[colorPtr->pixel]) { |
| 218 |
sprintf(string, "#%02x%02x%02x", (colorPtr->red >> 8), |
| 219 |
(colorPtr->green >> 8), (colorPtr->blue >> 8)); |
| 220 |
Tcl_AppendElement(interp, string); |
| 221 |
sprintf(string, "%ld", colorPtr->pixel); |
| 222 |
Tcl_AppendElement(interp, string); |
| 223 |
} |
| 224 |
} |
| 225 |
return TCL_OK; |
| 226 |
} |
| 227 |
|
| 228 |
#endif |
| 229 |
|
| 230 |
/*ARGSUSED*/ |
| 231 |
static int |
| 232 |
LowerOp(clientData, interp, argc, argv) |
| 233 |
ClientData clientData; |
| 234 |
Tcl_Interp *interp; |
| 235 |
int argc; /* Not used. */ |
| 236 |
char **argv; |
| 237 |
{ |
| 238 |
register int i; |
| 239 |
Window window; |
| 240 |
Display *display; |
| 241 |
|
| 242 |
display = Tk_Display(Tk_MainWindow(interp)); |
| 243 |
for (i = 2; i < argc; i++) { |
| 244 |
window = StringToWindow(interp, argv[i]); |
| 245 |
if (window == None) { |
| 246 |
return TCL_ERROR; |
| 247 |
} |
| 248 |
XLowerWindow(display, window); |
| 249 |
} |
| 250 |
return TCL_OK; |
| 251 |
} |
| 252 |
|
| 253 |
/*ARGSUSED*/ |
| 254 |
static int |
| 255 |
RaiseOp(clientData, interp, argc, argv) |
| 256 |
ClientData clientData; |
| 257 |
Tcl_Interp *interp; |
| 258 |
int argc; /* Not used. */ |
| 259 |
char **argv; |
| 260 |
{ |
| 261 |
register int i; |
| 262 |
Window window; |
| 263 |
Display *display; |
| 264 |
|
| 265 |
display = Tk_Display(Tk_MainWindow(interp)); |
| 266 |
for (i = 2; i < argc; i++) { |
| 267 |
window = StringToWindow(interp, argv[i]); |
| 268 |
if (window == None) { |
| 269 |
return TCL_ERROR; |
| 270 |
} |
| 271 |
XRaiseWindow(display, window); |
| 272 |
} |
| 273 |
return TCL_OK; |
| 274 |
} |
| 275 |
|
| 276 |
/*ARGSUSED*/ |
| 277 |
static int |
| 278 |
MapOp(clientData, interp, argc, argv) |
| 279 |
ClientData clientData; |
| 280 |
Tcl_Interp *interp; |
| 281 |
int argc; /* Not used. */ |
| 282 |
char **argv; |
| 283 |
{ |
| 284 |
register int i; |
| 285 |
Window window; |
| 286 |
Display *display; |
| 287 |
|
| 288 |
display = Tk_Display(Tk_MainWindow(interp)); |
| 289 |
for (i = 2; i < argc; i++) { |
| 290 |
if (argv[i][0] == '.') { |
| 291 |
Tk_Window tkwin; |
| 292 |
Tk_FakeWin *fakePtr; |
| 293 |
|
| 294 |
if (GetRealizedWindow(interp, argv[i], &tkwin) != TCL_OK) { |
| 295 |
return TCL_ERROR; |
| 296 |
} |
| 297 |
fakePtr = (Tk_FakeWin *) tkwin; |
| 298 |
fakePtr->flags |= TK_MAPPED; |
| 299 |
window = Tk_WindowId(tkwin); |
| 300 |
} else { |
| 301 |
int xid; |
| 302 |
|
| 303 |
if (Tcl_GetInt(interp, argv[i], &xid) != TCL_OK) { |
| 304 |
return TCL_ERROR; |
| 305 |
} |
| 306 |
window = (Window)xid; |
| 307 |
} |
| 308 |
XMapWindow(display, window); |
| 309 |
} |
| 310 |
return TCL_OK; |
| 311 |
} |
| 312 |
|
| 313 |
/*ARGSUSED*/ |
| 314 |
static int |
| 315 |
MoveOp(clientData, interp, argc, argv) |
| 316 |
ClientData clientData; |
| 317 |
Tcl_Interp *interp; |
| 318 |
int argc; /* Not Used. */ |
| 319 |
char **argv; |
| 320 |
{ |
| 321 |
int x, y; |
| 322 |
Tk_Window tkwin; |
| 323 |
Window window; |
| 324 |
Display *display; |
| 325 |
|
| 326 |
tkwin = Tk_MainWindow(interp); |
| 327 |
display = Tk_Display(tkwin); |
| 328 |
window = StringToWindow(interp, argv[2]); |
| 329 |
if (window == None) { |
| 330 |
return TCL_ERROR; |
| 331 |
} |
| 332 |
if (Tk_GetPixels(interp, tkwin, argv[3], &x) != TCL_OK) { |
| 333 |
Tcl_AppendResult(interp, ": bad window x-coordinate", (char *)NULL); |
| 334 |
return TCL_ERROR; |
| 335 |
} |
| 336 |
if (Tk_GetPixels(interp, tkwin, argv[4], &y) != TCL_OK) { |
| 337 |
Tcl_AppendResult(interp, ": bad window y-coordinate", (char *)NULL); |
| 338 |
return TCL_ERROR; |
| 339 |
} |
| 340 |
XMoveWindow(display, window, x, y); |
| 341 |
return TCL_OK; |
| 342 |
} |
| 343 |
|
| 344 |
/*ARGSUSED*/ |
| 345 |
static int |
| 346 |
UnmapOp(clientData, interp, argc, argv) |
| 347 |
ClientData clientData; |
| 348 |
Tcl_Interp *interp; |
| 349 |
int argc; /* Not used. */ |
| 350 |
char **argv; |
| 351 |
{ |
| 352 |
register int i; |
| 353 |
Window window; |
| 354 |
Display *display; |
| 355 |
|
| 356 |
display = Tk_Display(Tk_MainWindow(interp)); |
| 357 |
for (i = 2; i < argc; i++) { |
| 358 |
if (argv[i][0] == '.') { |
| 359 |
Tk_Window tkwin; |
| 360 |
Tk_FakeWin *fakePtr; |
| 361 |
|
| 362 |
if (GetRealizedWindow(interp, argv[i], &tkwin) != TCL_OK) { |
| 363 |
return TCL_ERROR; |
| 364 |
} |
| 365 |
fakePtr = (Tk_FakeWin *) tkwin; |
| 366 |
fakePtr->flags &= ~TK_MAPPED; |
| 367 |
window = Tk_WindowId(tkwin); |
| 368 |
} else { |
| 369 |
int xid; |
| 370 |
|
| 371 |
if (Tcl_GetInt(interp, argv[i], &xid) != TCL_OK) { |
| 372 |
return TCL_ERROR; |
| 373 |
} |
| 374 |
window = (Window)xid; |
| 375 |
} |
| 376 |
XMapWindow(display, window); |
| 377 |
} |
| 378 |
return TCL_OK; |
| 379 |
} |
| 380 |
|
| 381 |
/* ARGSUSED */ |
| 382 |
static int |
| 383 |
ChangesOp(clientData, interp, argc, argv) |
| 384 |
ClientData clientData; |
| 385 |
Tcl_Interp *interp; |
| 386 |
int argc; /* Not used. */ |
| 387 |
char **argv; /* Not used. */ |
| 388 |
{ |
| 389 |
Tk_Window tkwin; |
| 390 |
|
| 391 |
if (GetRealizedWindow(interp, argv[2], &tkwin) != TCL_OK) { |
| 392 |
return TCL_ERROR; |
| 393 |
} |
| 394 |
if (Tk_IsTopLevel(tkwin)) { |
| 395 |
XSetWindowAttributes attrs; |
| 396 |
Window window; |
| 397 |
unsigned int mask; |
| 398 |
|
| 399 |
window = Blt_GetRealWindowId(tkwin); |
| 400 |
attrs.backing_store = WhenMapped; |
| 401 |
attrs.save_under = True; |
| 402 |
mask = CWBackingStore | CWSaveUnder; |
| 403 |
XChangeWindowAttributes(Tk_Display(tkwin), window, mask, &attrs); |
| 404 |
} |
| 405 |
return TCL_OK; |
| 406 |
} |
| 407 |
|
| 408 |
/* ARGSUSED */ |
| 409 |
static int |
| 410 |
ParentOp(clientData, interp, argc, argv) |
| 411 |
ClientData clientData; |
| 412 |
Tcl_Interp *interp; |
| 413 |
int argc; /* Not used. */ |
| 414 |
char **argv; /* Not used. */ |
| 415 |
{ |
| 416 |
Window window; |
| 417 |
int value; |
| 418 |
char buf[50]; |
| 419 |
|
| 420 |
if (Tcl_GetInt(interp, argv[2], &value) != TCL_OK) { |
| 421 |
return TCL_ERROR; |
| 422 |
} |
| 423 |
window = Blt_GetParent(Tk_Display(Tk_MainWindow(interp)), (Window)value); |
| 424 |
if (window) { |
| 425 |
sprintf(buf, "0x%x", (int)window); |
| 426 |
Tcl_AppendResult(interp, buf, 0); |
| 427 |
} |
| 428 |
return TCL_OK; |
| 429 |
} |
| 430 |
|
| 431 |
/* ARGSUSED */ |
| 432 |
static int |
| 433 |
QueryOp(clientData, interp, argc, argv) |
| 434 |
ClientData clientData; |
| 435 |
Tcl_Interp *interp; |
| 436 |
int argc; /* Not used. */ |
| 437 |
char **argv; /* Not used. */ |
| 438 |
{ |
| 439 |
int rootX, rootY, childX, childY; |
| 440 |
Window root, child; |
| 441 |
unsigned int mask; |
| 442 |
Tk_Window tkwin = (Tk_Window)clientData; |
| 443 |
|
| 444 |
/* GetCursorPos */ |
| 445 |
if (XQueryPointer(Tk_Display(tkwin), Tk_WindowId(tkwin), &root, |
| 446 |
&child, &rootX, &rootY, &childX, &childY, &mask)) { |
| 447 |
char string[200]; |
| 448 |
|
| 449 |
sprintf(string, "@%d,%d", rootX, rootY); |
| 450 |
Tcl_SetResult(interp, string, TCL_VOLATILE); |
| 451 |
} |
| 452 |
return TCL_OK; |
| 453 |
} |
| 454 |
|
| 455 |
/*ARGSUSED*/ |
| 456 |
static int |
| 457 |
WarpToOp(clientData, interp, argc, argv) |
| 458 |
ClientData clientData; |
| 459 |
Tcl_Interp *interp; |
| 460 |
int argc; /* Not used. */ |
| 461 |
char **argv; |
| 462 |
{ |
| 463 |
Tk_Window tkwin, mainWindow; |
| 464 |
|
| 465 |
mainWindow = (Tk_Window)clientData; |
| 466 |
if (argc > 2) { |
| 467 |
if (argv[2][0] == '@') { |
| 468 |
int x, y; |
| 469 |
Window root; |
| 470 |
|
| 471 |
if (Blt_GetXY(interp, mainWindow, argv[2], &x, &y) != TCL_OK) { |
| 472 |
return TCL_ERROR; |
| 473 |
} |
| 474 |
root = RootWindow(Tk_Display(mainWindow), |
| 475 |
Tk_ScreenNumber(mainWindow)); |
| 476 |
XWarpPointer(Tk_Display(mainWindow), None, root, 0, 0, 0, 0, x, y); |
| 477 |
} else { |
| 478 |
if (GetRealizedWindow(interp, argv[2], &tkwin) != TCL_OK) { |
| 479 |
return TCL_ERROR; |
| 480 |
} |
| 481 |
if (!Tk_IsMapped(tkwin)) { |
| 482 |
Tcl_AppendResult(interp, "can't warp to unmapped window \"", |
| 483 |
Tk_PathName(tkwin), "\"", (char *)NULL); |
| 484 |
return TCL_ERROR; |
| 485 |
} |
| 486 |
XWarpPointer(Tk_Display(tkwin), None, Tk_WindowId(tkwin), |
| 487 |
0, 0, 0, 0, Tk_Width(tkwin) / 2, Tk_Height(tkwin) / 2); |
| 488 |
} |
| 489 |
} |
| 490 |
return QueryOp(clientData, interp, 0, (char **)NULL); |
| 491 |
} |
| 492 |
|
| 493 |
#ifdef notdef |
| 494 |
static int |
| 495 |
ReparentOp(clientData, interp, argc, argv) |
| 496 |
ClientData clientData; |
| 497 |
Tcl_Interp *interp; |
| 498 |
int argc; |
| 499 |
char **argv; |
| 500 |
{ |
| 501 |
Tk_Window tkwin; |
| 502 |
|
| 503 |
if (GetRealizedWindow(interp, argv[2], &tkwin) != TCL_OK) { |
| 504 |
return TCL_ERROR; |
| 505 |
} |
| 506 |
if (argc == 4) { |
| 507 |
Tk_Window newParent; |
| 508 |
|
| 509 |
if (GetRealizedWindow(interp, argv[3], &newParent) != TCL_OK) { |
| 510 |
return TCL_ERROR; |
| 511 |
} |
| 512 |
Blt_RelinkWindow2(tkwin, Blt_GetRealWindowId(tkwin), newParent, 0, 0); |
| 513 |
} else { |
| 514 |
Blt_UnlinkWindow(tkwin); |
| 515 |
} |
| 516 |
return TCL_OK; |
| 517 |
} |
| 518 |
#endif |
| 519 |
|
| 520 |
|
| 521 |
/* |
| 522 |
* This is a temporary home for these image routines. They will be |
| 523 |
* moved when a new image type is created for them. |
| 524 |
*/ |
| 525 |
/*ARGSUSED*/ |
| 526 |
static int |
| 527 |
ConvolveOp(clientData, interp, argc, argv) |
| 528 |
ClientData clientData; /* Not used. */ |
| 529 |
Tcl_Interp *interp; |
| 530 |
int argc; /* Not used. */ |
| 531 |
char **argv; |
| 532 |
{ |
| 533 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 534 |
Blt_ColorImage srcImage, destImage; |
| 535 |
Filter2D filter; |
| 536 |
int nValues; |
| 537 |
char **valueArr; |
| 538 |
double *kernel; |
| 539 |
double value, sum; |
| 540 |
register int i; |
| 541 |
int dim; |
| 542 |
int result = TCL_ERROR; |
| 543 |
|
| 544 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 545 |
if (srcPhoto == NULL) { |
| 546 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 547 |
" exist or is not a photo image", (char *)NULL); |
| 548 |
return TCL_ERROR; |
| 549 |
} |
| 550 |
destPhoto = Blt_FindPhoto(interp, argv[3]); |
| 551 |
if (destPhoto == NULL) { |
| 552 |
Tcl_AppendResult(interp, "destination image \"", argv[3], "\" doesn't", |
| 553 |
" exist or is not a photo image", (char *)NULL); |
| 554 |
return TCL_ERROR; |
| 555 |
} |
| 556 |
if (Tcl_SplitList(interp, argv[4], &nValues, &valueArr) != TCL_OK) { |
| 557 |
return TCL_ERROR; |
| 558 |
} |
| 559 |
kernel = NULL; |
| 560 |
if (nValues == 0) { |
| 561 |
Tcl_AppendResult(interp, "empty kernel", (char *)NULL); |
| 562 |
goto error; |
| 563 |
} |
| 564 |
dim = (int)sqrt((double)nValues); |
| 565 |
if ((dim * dim) != nValues) { |
| 566 |
Tcl_AppendResult(interp, "kernel must be square", (char *)NULL); |
| 567 |
goto error; |
| 568 |
} |
| 569 |
kernel = Blt_Malloc(sizeof(double) * nValues); |
| 570 |
sum = 0.0; |
| 571 |
for (i = 0; i < nValues; i++) { |
| 572 |
if (Tcl_GetDouble(interp, valueArr[i], &value) != TCL_OK) { |
| 573 |
goto error; |
| 574 |
} |
| 575 |
kernel[i] = value; |
| 576 |
sum += value; |
| 577 |
} |
| 578 |
filter.kernel = kernel; |
| 579 |
filter.support = dim * 0.5; |
| 580 |
filter.sum = (sum == 0.0) ? 1.0 : sum; |
| 581 |
filter.scale = 1.0 / nValues; |
| 582 |
|
| 583 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 584 |
destImage = Blt_ConvolveColorImage(srcImage, &filter); |
| 585 |
Blt_FreeColorImage(srcImage); |
| 586 |
Blt_ColorImageToPhoto(destImage, destPhoto); |
| 587 |
Blt_FreeColorImage(destImage); |
| 588 |
result = TCL_OK; |
| 589 |
error: |
| 590 |
if (valueArr != NULL) { |
| 591 |
Blt_Free(valueArr); |
| 592 |
} |
| 593 |
if (kernel != NULL) { |
| 594 |
Blt_Free(kernel); |
| 595 |
} |
| 596 |
return result; |
| 597 |
} |
| 598 |
|
| 599 |
/*ARGSUSED*/ |
| 600 |
static int |
| 601 |
QuantizeOp(clientData, interp, argc, argv) |
| 602 |
ClientData clientData; /* Not used. */ |
| 603 |
Tcl_Interp *interp; |
| 604 |
int argc; /* Not used. */ |
| 605 |
char **argv; |
| 606 |
{ |
| 607 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 608 |
Tk_PhotoImageBlock src, dest; |
| 609 |
Blt_ColorImage srcImage, destImage; |
| 610 |
int nColors = 1; |
| 611 |
int result; |
| 612 |
|
| 613 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 614 |
if (srcPhoto == NULL) { |
| 615 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 616 |
" exist or is not a photo image", (char *)NULL); |
| 617 |
return TCL_ERROR; |
| 618 |
} |
| 619 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 620 |
if ((src.width <= 1) || (src.height <= 1)) { |
| 621 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" is empty", |
| 622 |
(char *)NULL); |
| 623 |
return TCL_ERROR; |
| 624 |
} |
| 625 |
destPhoto = Blt_FindPhoto(interp, argv[3]); |
| 626 |
if (destPhoto == NULL) { |
| 627 |
Tcl_AppendResult(interp, "destination image \"", argv[3], "\" doesn't", |
| 628 |
" exist or is not a photo image", (char *)NULL); |
| 629 |
return TCL_ERROR; |
| 630 |
} |
| 631 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 632 |
if ((dest.width != src.width) || (dest.height != src.height)) { |
| 633 |
Tk_PhotoSetSize(destPhoto, src.width, src.height); |
| 634 |
} |
| 635 |
if (argc>4) { |
| 636 |
if (Tcl_GetInt(interp, argv[4], &nColors) != TCL_OK) { |
| 637 |
return TCL_ERROR; |
| 638 |
} |
| 639 |
} |
| 640 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 641 |
destImage = Blt_PhotoToColorImage(destPhoto); |
| 642 |
result = Blt_QuantizeColorImage(srcImage, destImage, nColors); |
| 643 |
if (result == TCL_OK) { |
| 644 |
Blt_ColorImageToPhoto(destImage, destPhoto); |
| 645 |
} |
| 646 |
Blt_FreeColorImage(srcImage); |
| 647 |
Blt_FreeColorImage(destImage); |
| 648 |
return result; |
| 649 |
} |
| 650 |
|
| 651 |
static int |
| 652 |
GetColorPix32(Tcl_Interp *interp, Tk_Window tkwin, char *color, Pix32 *colPtr) { |
| 653 |
XColor *xCol; |
| 654 |
int red, green, blue; |
| 655 |
|
| 656 |
colPtr->Alpha = 255; |
| 657 |
if (*color == '#' && strlen(color) == 7 && 3 == |
| 658 |
sscanf(color+1, "%02x%02x%02x", &red, &green, &blue)) { |
| 659 |
colPtr->Red = red; |
| 660 |
colPtr->Green = green; |
| 661 |
colPtr->Blue = blue; |
| 662 |
return TCL_OK; |
| 663 |
} |
| 664 |
xCol = Tk_GetColor(interp, tkwin, Tk_GetUid(color)); |
| 665 |
if (xCol == NULL) { |
| 666 |
return TCL_ERROR; |
| 667 |
} |
| 668 |
colPtr->Red = (xCol->red >> 8); |
| 669 |
colPtr->Green = (xCol->green >> 8); |
| 670 |
colPtr->Blue = (xCol->blue >> 8); |
| 671 |
return TCL_OK; |
| 672 |
} |
| 673 |
|
| 674 |
static int |
| 675 |
XColorToPix32(XColor *xCol, Pix32 *colPtr) { |
| 676 |
|
| 677 |
colPtr->Alpha = 255; |
| 678 |
colPtr->Red = (xCol->red >> 8); |
| 679 |
colPtr->Green = (xCol->green >> 8); |
| 680 |
colPtr->Blue = (xCol->blue >> 8); |
| 681 |
return TCL_OK; |
| 682 |
} |
| 683 |
|
| 684 |
/*ARGSUSED*/ |
| 685 |
static int |
| 686 |
AlphaOp(clientData, interp, argc, argv) |
| 687 |
ClientData clientData; /* Not used. */ |
| 688 |
Tcl_Interp *interp; |
| 689 |
int argc; /* Not used. */ |
| 690 |
char **argv; |
| 691 |
{ |
| 692 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 693 |
Tk_PhotoImageBlock src, dest; |
| 694 |
Blt_ColorImage srcImage, destImage; |
| 695 |
int result, alpha, anycolor = 0, withAlpha, hasWith = 0; |
| 696 |
Tk_Window tkwin = (Tk_Window)clientData; |
| 697 |
Pix32 oldColor; |
| 698 |
char *string; |
| 699 |
int negate = 0, shift = 0; |
| 700 |
|
| 701 |
alpha = 0; |
| 702 |
if (!strcmp("-shift", argv[2])) { |
| 703 |
shift = 1; |
| 704 |
argc -= 1; |
| 705 |
argv += 1; |
| 706 |
} |
| 707 |
|
| 708 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 709 |
if (srcPhoto == NULL) { |
| 710 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 711 |
" exist or is not a photo image", (char *)NULL); |
| 712 |
return TCL_ERROR; |
| 713 |
} |
| 714 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 715 |
if ((src.width <= 1) || (src.height <= 1)) { |
| 716 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" is empty", |
| 717 |
(char *)NULL); |
| 718 |
return TCL_ERROR; |
| 719 |
} |
| 720 |
destPhoto = Blt_FindPhoto(interp, argv[3]); |
| 721 |
if (destPhoto == NULL) { |
| 722 |
Tcl_AppendResult(interp, "destination image \"", argv[3], "\" doesn't", |
| 723 |
" exist or is not a photo image", (char *)NULL); |
| 724 |
return TCL_ERROR; |
| 725 |
} |
| 726 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 727 |
if (0 && srcPhoto == destPhoto) { |
| 728 |
Tcl_AppendResult(interp, "src and dest images must be different", |
| 729 |
(char *)NULL); |
| 730 |
return TCL_ERROR; |
| 731 |
} |
| 732 |
string = argv[4]; |
| 733 |
if (string[0] == '!') { |
| 734 |
negate = 1; |
| 735 |
string++; |
| 736 |
} |
| 737 |
if (!strcmp(string, "*")) { |
| 738 |
anycolor = 1; |
| 739 |
if (argc <= 5) { |
| 740 |
Tcl_AppendResult(interp, "must give an alpha", 0); |
| 741 |
return TCL_ERROR; |
| 742 |
} |
| 743 |
|
| 744 |
} else { |
| 745 |
if (GetColorPix32(interp, tkwin, string, &oldColor) != TCL_OK) { |
| 746 |
return TCL_ERROR; |
| 747 |
} |
| 748 |
} |
| 749 |
if (argc > 5) { |
| 750 |
if (Tcl_GetInt(interp, argv[5], &alpha) != TCL_OK) { |
| 751 |
return TCL_ERROR; |
| 752 |
} |
| 753 |
if (alpha<0 || alpha > 255) { |
| 754 |
Tcl_AppendResult(interp, "alpha must be >= 0 and <= 255", argv[3], |
| 755 |
(char *)NULL); |
| 756 |
return TCL_ERROR; |
| 757 |
} |
| 758 |
} |
| 759 |
if (argc > 6) { |
| 760 |
if (Tcl_GetInt(interp, argv[6], &withAlpha) != TCL_OK) { |
| 761 |
return TCL_ERROR; |
| 762 |
} |
| 763 |
hasWith = 1; |
| 764 |
if (withAlpha<0 || withAlpha > 255) { |
| 765 |
Tcl_AppendResult(interp, "withalpha must be >= 0 and <= 255", argv[3], |
| 766 |
(char *)NULL); |
| 767 |
return TCL_ERROR; |
| 768 |
} |
| 769 |
} |
| 770 |
if ((dest.width != src.width) || (dest.height != src.height)) { |
| 771 |
Tk_PhotoSetSize(destPhoto, src.width, src.height); |
| 772 |
} |
| 773 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 774 |
destImage = Blt_PhotoToColorImage(destPhoto); |
| 775 |
result = TCL_OK; |
| 776 |
{ |
| 777 |
int width, height; |
| 778 |
int count, same; |
| 779 |
Pix32 *srcPtr, *destPtr, *endPtr, *color; |
| 780 |
unsigned char origAlpha; |
| 781 |
|
| 782 |
width = Blt_ColorImageWidth(srcImage); |
| 783 |
height = Blt_ColorImageHeight(srcImage); |
| 784 |
count = width * height; |
| 785 |
color = &oldColor; |
| 786 |
|
| 787 |
srcPtr = Blt_ColorImageBits(srcImage); |
| 788 |
destPtr = Blt_ColorImageBits(destImage); |
| 789 |
if (shift) { |
| 790 |
for (endPtr = destPtr + count; destPtr < endPtr; srcPtr++, destPtr++) { |
| 791 |
origAlpha = srcPtr->Alpha; |
| 792 |
if (origAlpha == 0) { |
| 793 |
destPtr->value = srcPtr->value; |
| 794 |
continue; |
| 795 |
} |
| 796 |
destPtr->value = color->value; |
| 797 |
destPtr->Alpha = srcPtr->Blue; |
| 798 |
} |
| 799 |
} else if (!anycolor) { |
| 800 |
color = &oldColor; |
| 801 |
for (endPtr = destPtr + count; destPtr < endPtr; srcPtr++, destPtr++) { |
| 802 |
origAlpha = srcPtr->Alpha; |
| 803 |
destPtr->value = srcPtr->value; |
| 804 |
same = (srcPtr->Red == color->Red && srcPtr->Green == color->Green && |
| 805 |
srcPtr->Blue == color->Blue); |
| 806 |
if (hasWith && origAlpha != withAlpha) { |
| 807 |
|
| 808 |
} else if (negate) { |
| 809 |
if ((!same) && (origAlpha != (unsigned char)-1)) { |
| 810 |
origAlpha = alpha; |
| 811 |
} |
| 812 |
} else { |
| 813 |
if (same) { |
| 814 |
origAlpha = alpha; |
| 815 |
} |
| 816 |
} |
| 817 |
destPtr->Alpha = origAlpha; |
| 818 |
} |
| 819 |
} else { |
| 820 |
for (endPtr = destPtr + count; destPtr < endPtr; srcPtr++, destPtr++) { |
| 821 |
origAlpha = srcPtr->Alpha; |
| 822 |
destPtr->value = srcPtr->value; |
| 823 |
if (hasWith && origAlpha == withAlpha) { |
| 824 |
destPtr->Alpha = alpha; |
| 825 |
} else if (origAlpha == (unsigned char)-1) { |
| 826 |
destPtr->Alpha = alpha; |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
if (result == TCL_OK) { |
| 833 |
Blt_ColorImageToPhoto(destImage, destPhoto); |
| 834 |
} |
| 835 |
Blt_FreeColorImage(srcImage); |
| 836 |
Blt_FreeColorImage(destImage); |
| 837 |
return result; |
| 838 |
} |
| 839 |
|
| 840 |
/*ARGSUSED*/ |
| 841 |
static int |
| 842 |
RecolorOp(clientData, interp, argc, argv) |
| 843 |
ClientData clientData; /* Not used. */ |
| 844 |
Tcl_Interp *interp; |
| 845 |
int argc; /* Not used. */ |
| 846 |
char **argv; |
| 847 |
{ |
| 848 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 849 |
Tk_PhotoImageBlock src, dest; |
| 850 |
Blt_ColorImage srcImage, destImage; |
| 851 |
int result, alpha; |
| 852 |
Tk_Window tkwin = (Tk_Window)clientData; |
| 853 |
Pix32 oldColor, newColor; |
| 854 |
|
| 855 |
alpha = -1; |
| 856 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 857 |
if (srcPhoto == NULL) { |
| 858 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 859 |
" exist or is not a photo image", (char *)NULL); |
| 860 |
return TCL_ERROR; |
| 861 |
} |
| 862 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 863 |
if ((src.width <= 1) || (src.height <= 1)) { |
| 864 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" is empty", |
| 865 |
(char *)NULL); |
| 866 |
return TCL_ERROR; |
| 867 |
} |
| 868 |
destPhoto = Blt_FindPhoto(interp, argv[3]); |
| 869 |
if (destPhoto == NULL) { |
| 870 |
Tcl_AppendResult(interp, "destination image \"", argv[3], "\" doesn't", |
| 871 |
" exist or is not a photo image", (char *)NULL); |
| 872 |
return TCL_ERROR; |
| 873 |
} |
| 874 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 875 |
if (GetColorPix32(interp, tkwin, argv[4], &oldColor) != TCL_OK) { |
| 876 |
return TCL_ERROR; |
| 877 |
} |
| 878 |
if (GetColorPix32(interp, tkwin, argv[5], &newColor) != TCL_OK) { |
| 879 |
return TCL_ERROR; |
| 880 |
} |
| 881 |
if (argc > 6) { |
| 882 |
if (Tcl_GetInt(interp, argv[6], &alpha) != TCL_OK) { |
| 883 |
return TCL_ERROR; |
| 884 |
} |
| 885 |
if (alpha<0 || alpha > 255) { |
| 886 |
Tcl_AppendResult(interp, "alpha must be >= 0 and <= 255", argv[3], |
| 887 |
(char *)NULL); |
| 888 |
return TCL_ERROR; |
| 889 |
} |
| 890 |
} |
| 891 |
if ((dest.width != src.width) || (dest.height != src.height)) { |
| 892 |
Tk_PhotoSetSize(destPhoto, src.width, src.height); |
| 893 |
} |
| 894 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 895 |
destImage = Blt_PhotoToColorImage(destPhoto); |
| 896 |
result = Blt_RecolorImage(srcImage, destImage, &oldColor, &newColor, alpha); |
| 897 |
if (result == TCL_OK) { |
| 898 |
Blt_ColorImageToPhoto(destImage, destPhoto); |
| 899 |
} |
| 900 |
Blt_FreeColorImage(srcImage); |
| 901 |
Blt_FreeColorImage(destImage); |
| 902 |
return result; |
| 903 |
} |
| 904 |
|
| 905 |
/*ARGSUSED*/ |
| 906 |
static int |
| 907 |
ColorsOp(clientData, interp, argc, argv) |
| 908 |
ClientData clientData; /* Not used. */ |
| 909 |
Tcl_Interp *interp; |
| 910 |
int argc; /* Not used. */ |
| 911 |
char **argv; |
| 912 |
{ |
| 913 |
Tk_PhotoHandle srcPhoto; |
| 914 |
Tk_PhotoImageBlock src; |
| 915 |
Blt_ColorImage srcImage; |
| 916 |
int x, y, isalph, iscnt, isNew, cnt; |
| 917 |
int i, rng[4], from = 0; |
| 918 |
register Pix32 *srcPtr; |
| 919 |
Tcl_Obj *listPtr; |
| 920 |
char buf[100]; |
| 921 |
Blt_HashEntry *hPtr; |
| 922 |
Blt_HashTable hTbl; |
| 923 |
Blt_HashSearch cursor; |
| 924 |
|
| 925 |
isalph = 0; |
| 926 |
iscnt = 0; |
| 927 |
while (argc > 3) { |
| 928 |
if (!strcmp(argv[2], "-alpha")) { |
| 929 |
isalph = 1; |
| 930 |
} else if (!strcmp(argv[2], "-from")) { |
| 931 |
from = 1; |
| 932 |
if (argc<7) { |
| 933 |
Tcl_AppendResult(interp, "expected 4 args: x1 y1 x2 y2", (char *)NULL); |
| 934 |
return TCL_ERROR; |
| 935 |
} |
| 936 |
for (i=0; i<4; i++) { |
| 937 |
if (Tcl_GetInt(interp, argv[i+3], rng+i)) { |
| 938 |
return TCL_ERROR; |
| 939 |
} |
| 940 |
} |
| 941 |
argc -= 4; |
| 942 |
argv += 4; |
| 943 |
} else if (!strcmp(argv[2], "-count")) { |
| 944 |
iscnt = 1; |
| 945 |
} else { |
| 946 |
Tcl_AppendResult(interp, "expected -from, -alpha or -count", (char *)NULL); |
| 947 |
return TCL_ERROR; |
| 948 |
} |
| 949 |
argc--; |
| 950 |
argv++; |
| 951 |
} |
| 952 |
if (argc != 3) { |
| 953 |
Tcl_AppendResult(interp, "too few arguments", (char *)NULL); |
| 954 |
return TCL_ERROR; |
| 955 |
} |
| 956 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 957 |
if (srcPhoto == NULL) { |
| 958 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 959 |
" exist or is not a photo image", (char *)NULL); |
| 960 |
return TCL_ERROR; |
| 961 |
} |
| 962 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 963 |
if ((src.width < 1) || (src.height < 1)) { |
| 964 |
return TCL_OK; |
| 965 |
} |
| 966 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 967 |
srcPtr = Blt_ColorImageBits(srcImage); |
| 968 |
|
| 969 |
Blt_InitHashTable(&hTbl, BLT_STRING_KEYS); |
| 970 |
for (y = 0; y < src.height; y++) { |
| 971 |
if (from && (y < rng[1] || y > rng[3])) continue; |
| 972 |
for (x = 0; x < src.width; x++) { |
| 973 |
if (from && (x < rng[0] || x > rng[2])) continue; |
| 974 |
if (!isalph) { |
| 975 |
sprintf(buf, "#%02x%02x%02x", srcPtr->Red, srcPtr->Green, srcPtr->Blue); |
| 976 |
} else { |
| 977 |
sprintf(buf, "#%02x%02x%02x:%02x", srcPtr->Red, srcPtr->Green, srcPtr->Blue, srcPtr->Alpha); |
| 978 |
} |
| 979 |
hPtr = Blt_CreateHashEntry(&hTbl, buf, &isNew); |
| 980 |
if (isNew) { |
| 981 |
Blt_SetHashValue(hPtr, 1); |
| 982 |
} else { |
| 983 |
cnt = (int)Blt_GetHashValue(hPtr); |
| 984 |
cnt++; |
| 985 |
Blt_SetHashValue(hPtr, cnt); |
| 986 |
} |
| 987 |
srcPtr++; |
| 988 |
} |
| 989 |
} |
| 990 |
listPtr = Tcl_NewListObj(0,0); |
| 991 |
for (hPtr = Blt_FirstHashEntry(&hTbl, &cursor); |
| 992 |
hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) { |
| 993 |
|
| 994 |
Tcl_Obj *objPtr = Tcl_NewStringObj(Blt_GetHashKey(&hTbl, hPtr), -1); |
| 995 |
Tcl_ListObjAppendElement(interp, listPtr, objPtr); |
| 996 |
if (iscnt) { |
| 997 |
cnt = (int)Blt_GetHashValue(hPtr); |
| 998 |
sprintf(buf, "%d", cnt); |
| 999 |
objPtr = Tcl_NewStringObj(buf, -1); |
| 1000 |
Tcl_ListObjAppendElement(interp, listPtr, objPtr); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
Tcl_SetObjResult(interp, listPtr); |
| 1004 |
Blt_DeleteHashTable(&hTbl); |
| 1005 |
return TCL_OK; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/*ARGSUSED*/ |
| 1009 |
static int |
| 1010 |
TransOp(clientData, interp, argc, argv) |
| 1011 |
ClientData clientData; /* Not used. */ |
| 1012 |
Tcl_Interp *interp; |
| 1013 |
int argc; /* Not used. */ |
| 1014 |
char **argv; |
| 1015 |
{ |
| 1016 |
Tk_PhotoHandle srcPhoto; |
| 1017 |
Tk_PhotoImageBlock src; |
| 1018 |
Blt_ColorImage srcImage; |
| 1019 |
int x, y, alpha,isSet = 0; |
| 1020 |
register Pix32 *srcPtr; |
| 1021 |
char buf[100]; |
| 1022 |
|
| 1023 |
if (argc == 6) { |
| 1024 |
isSet = 1; |
| 1025 |
if (Tcl_GetInt(interp, argv[5], &alpha) != TCL_OK) { |
| 1026 |
return TCL_ERROR; |
| 1027 |
} |
| 1028 |
if (alpha<0 || alpha > 255) { |
| 1029 |
Tcl_AppendResult(interp, "alpha must be >= 0 and <= 255", argv[3], |
| 1030 |
(char *)NULL); |
| 1031 |
return TCL_ERROR; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
if (Tcl_GetInt(interp, argv[3], &x) != TCL_OK) { |
| 1035 |
return TCL_ERROR; |
| 1036 |
} |
| 1037 |
if (Tcl_GetInt(interp, argv[4], &y) != TCL_OK) { |
| 1038 |
return TCL_ERROR; |
| 1039 |
} |
| 1040 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 1041 |
if (srcPhoto == NULL) { |
| 1042 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 1043 |
" exist or is not a photo image", (char *)NULL); |
| 1044 |
return TCL_ERROR; |
| 1045 |
} |
| 1046 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 1047 |
if ((src.width < 1) || (src.height < 1)) { |
| 1048 |
Tcl_AppendResult(interp, "empty image", (char *)NULL); |
| 1049 |
return TCL_ERROR; |
| 1050 |
} |
| 1051 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 1052 |
srcPtr = Blt_ColorImageBits(srcImage); |
| 1053 |
if (y < 0 || y >= src.height || x < 0 || x >= src.width) { |
| 1054 |
Tcl_AppendResult(interp, "out of range", (char *)NULL); |
| 1055 |
return TCL_ERROR; |
| 1056 |
} |
| 1057 |
srcPtr = srcPtr + y*src.width + x; |
| 1058 |
if (isSet) { |
| 1059 |
srcPtr->Alpha = alpha; |
| 1060 |
Blt_ColorImageToPhoto(srcImage, srcPhoto); |
| 1061 |
} else { |
| 1062 |
sprintf(buf, "%d", srcPtr->Alpha); |
| 1063 |
Tcl_AppendResult(interp, buf, 0); |
| 1064 |
} |
| 1065 |
return TCL_OK; |
| 1066 |
} |
| 1067 |
|
| 1068 |
#define PD_SRC_OVER(srcColor,srcAlpha,dstColor,dstAlpha) \ |
| 1069 |
(srcColor*srcAlpha/255) + dstAlpha*(255-srcAlpha)/255*dstColor/255 |
| 1070 |
#define PD_SRC_OVER_ALPHA(srcAlpha,dstAlpha) \ |
| 1071 |
(srcAlpha + (255-srcAlpha)*dstAlpha/255) |
| 1072 |
|
| 1073 |
static |
| 1074 |
void PixBlend( |
| 1075 |
Pix32 *srcPtr, Pix32 *src2Ptr, Pix32 *destPtr, |
| 1076 |
unsigned char alpha, unsigned char dAlpha |
| 1077 |
) { |
| 1078 |
dAlpha = 255-alpha; |
| 1079 |
destPtr->Red = PD_SRC_OVER(srcPtr->Red, alpha, src2Ptr->Red, dAlpha); |
| 1080 |
destPtr->Green = PD_SRC_OVER(srcPtr->Green, alpha, src2Ptr->Green, dAlpha); |
| 1081 |
destPtr->Blue = PD_SRC_OVER(srcPtr->Blue, alpha, src2Ptr->Blue, dAlpha); |
| 1082 |
destPtr->Alpha = 255; /* PD_SRC_OVER_ALPHA(alpha, dAlpha); */ |
| 1083 |
} |
| 1084 |
|
| 1085 |
static |
| 1086 |
void PixMerged( Pix32 *srcPtr, Pix32 *src2Ptr, Pix32 *destPtr, Pix32 *colorPtr) { |
| 1087 |
unsigned char alpha; |
| 1088 |
int isdead; |
| 1089 |
|
| 1090 |
alpha = srcPtr->Blue; |
| 1091 |
isdead = (srcPtr->Red == 0xde && srcPtr->Green == 0xad); |
| 1092 |
if (alpha == 0 && isdead) { /* "," inner fill */ |
| 1093 |
destPtr->value = src2Ptr->value; |
| 1094 |
} else if (alpha == 0) { /* "." none or transparent */ |
| 1095 |
destPtr->value = 0; |
| 1096 |
} else if (isdead == 0) { /* solid color with alpha */ |
| 1097 |
destPtr->value = colorPtr->value; |
| 1098 |
destPtr->Alpha = alpha; |
| 1099 |
} else { /* Blend color with inner fill */ |
| 1100 |
PixBlend( colorPtr, src2Ptr, destPtr, alpha, src2Ptr->Alpha); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
/* For RGB grab alpha from blue and substitute maskcolor for RG="#DEAD"*/ |
| 1106 |
int Blt_ImageMergeInner(Tcl_Interp *interp, char *srcName, char *src2Name, |
| 1107 |
char * destName, XColor *maskColor, int leaveMsg) { |
| 1108 |
Tk_PhotoHandle srcPhoto, srcPhoto2, destPhoto; |
| 1109 |
Tk_PhotoImageBlock src, src2, dest; |
| 1110 |
Blt_ColorImage srcImage, srcImage2, destImage; |
| 1111 |
double opacity; |
| 1112 |
double opacity2; |
| 1113 |
int result, isWc; |
| 1114 |
Pix32 withColor; |
| 1115 |
|
| 1116 |
isWc = 0; |
| 1117 |
opacity = 0.5; |
| 1118 |
opacity2 = -1; |
| 1119 |
srcPhoto = Blt_FindPhoto(interp, srcName); |
| 1120 |
if (srcPhoto == NULL) { |
| 1121 |
if (leaveMsg) |
| 1122 |
Tcl_AppendResult(interp, "source image \"", srcName, "\" doesn't", |
| 1123 |
" exist or is not a photo image", (char *)NULL); |
| 1124 |
return TCL_ERROR; |
| 1125 |
} |
| 1126 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 1127 |
if ((src.width <= 1) || (src.height <= 1)) { |
| 1128 |
if (leaveMsg) |
| 1129 |
Tcl_AppendResult(interp, "source image \"", srcName, "\" is empty", |
| 1130 |
(char *)NULL); |
| 1131 |
return TCL_ERROR; |
| 1132 |
} |
| 1133 |
|
| 1134 |
srcPhoto2 = Blt_FindPhoto(interp, src2Name); |
| 1135 |
if (srcPhoto2 == NULL) { |
| 1136 |
if (leaveMsg) |
| 1137 |
Tcl_AppendResult(interp, "source image \"", src2Name, "\" doesn't", |
| 1138 |
" exist or is not a photo image", (char *)NULL); |
| 1139 |
return TCL_ERROR; |
| 1140 |
} |
| 1141 |
Tk_PhotoGetImage(srcPhoto2, &src2); |
| 1142 |
if ((src2.width <= 1) || (src2.height <= 1)) { |
| 1143 |
if (leaveMsg) |
| 1144 |
Tcl_AppendResult(interp, "source image \"", src2Name, "\" is empty", |
| 1145 |
(char *)NULL); |
| 1146 |
return TCL_ERROR; |
| 1147 |
} |
| 1148 |
if (maskColor) { |
| 1149 |
XColorToPix32(maskColor, &withColor); |
| 1150 |
isWc = 1; |
| 1151 |
goto domerge; |
| 1152 |
} |
| 1153 |
/* |
| 1154 |
if (argc>5) { |
| 1155 |
|
| 1156 |
if (isdigit(argv[5][0]) == 0 && argv[5][0] != '.') { |
| 1157 |
Tk_Window tkwin; |
| 1158 |
char *colStr; |
| 1159 |
|
| 1160 |
tkwin = Tk_MainWindow(interp); |
| 1161 |
isWc = 1; |
| 1162 |
|
| 1163 |
colStr = argv[5]; |
| 1164 |
if (colStr[0] == '!') { |
| 1165 |
colStr++; |
| 1166 |
isWc = 2; |
| 1167 |
} |
| 1168 |
|
| 1169 |
return TCL_ERROR; |
| 1170 |
} |
| 1171 |
if (src.width < 4 || src.height < 4) { |
| 1172 |
Tcl_AppendResult(interp, "src image too small ", 0); |
| 1173 |
return TCL_ERROR; |
| 1174 |
} |
| 1175 |
if (src2.width < 4 || src2.height < 4) { |
| 1176 |
Tcl_AppendResult(interp, "src2 image too small ", 0); |
| 1177 |
return TCL_ERROR; |
| 1178 |
} |
| 1179 |
if (dest.width < 4 || dest.height < 4) { |
| 1180 |
Tcl_AppendResult(interp, "dest image too small ", 0); |
| 1181 |
return TCL_ERROR; |
| 1182 |
} |
| 1183 |
goto domerge; |
| 1184 |
} |
| 1185 |
|
| 1186 |
if (Tcl_GetDouble(interp, argv[5], &opacity) != TCL_OK) { |
| 1187 |
return TCL_ERROR; |
| 1188 |
} |
| 1189 |
if (opacity < 0.0 || opacity > 1.0) { |
| 1190 |
Tcl_AppendResult(interp, "opacity must be >= 0.0 and <= 1.0: ", argv[5], (char *)NULL); |
| 1191 |
return TCL_ERROR; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
if (argc>6) { |
| 1195 |
if (Tcl_GetDouble(interp, argv[6], &opacity2) != TCL_OK) { |
| 1196 |
return TCL_ERROR; |
| 1197 |
} |
| 1198 |
if (opacity2 < 0.0 || opacity2 > 1.0) { |
| 1199 |
Tcl_AppendResult(interp, "opacity must be >= 0.0 and <= 1.0: ", argv[6], (char *)NULL); |
| 1200 |
return TCL_ERROR; |
| 1201 |
} |
| 1202 |
} |
| 1203 |
*/ |
| 1204 |
if (isWc == 0 && ((src2.width != src.width) || (src2.height != src.height))) { |
| 1205 |
int sh, sw; |
| 1206 |
sw = (src2.width>src.width?src2.width:src.width); |
| 1207 |
sh = (src2.height>src.height?src2.height:src.height); |
| 1208 |
Tk_PhotoSetSize(srcPhoto2, sw, sh); |
| 1209 |
Tk_PhotoGetImage(srcPhoto2, &src2); |
| 1210 |
if (sw != src.width || sh != src.height) { |
| 1211 |
Tk_PhotoSetSize(srcPhoto, sw, sh); |
| 1212 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
domerge: |
| 1217 |
destPhoto = Blt_FindPhoto(interp, destName); |
| 1218 |
if (destPhoto == NULL) { |
| 1219 |
if (leaveMsg) |
| 1220 |
Tcl_AppendResult(interp, "destination image \"", destName, "\" doesn't", |
| 1221 |
" exist or is not a photo image", (char *)NULL); |
| 1222 |
return TCL_ERROR; |
| 1223 |
} |
| 1224 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 1225 |
|
| 1226 |
if ((dest.width != src.width) || (dest.height != src.height)) { |
| 1227 |
Tk_PhotoSetSize(destPhoto, src.width, src.height); |
| 1228 |
} |
| 1229 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 1230 |
srcImage2 = Blt_PhotoToColorImage(srcPhoto2); |
| 1231 |
destImage = Blt_PhotoToColorImage(destPhoto); |
| 1232 |
|
| 1233 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 1234 |
if ((dest.width != src.width) || (dest.height != src.height)) { |
| 1235 |
Tk_PhotoSetSize(destPhoto, src.width, src.height); |
| 1236 |
destImage = Blt_PhotoToColorImage(destPhoto); |
| 1237 |
} |
| 1238 |
if (isWc != 1) { |
| 1239 |
result = Blt_MergeColorImage(srcImage, srcImage2, destImage, opacity, opacity2, |
| 1240 |
isWc ? &withColor : NULL); |
| 1241 |
} else { |
| 1242 |
int xs1, xs2, xd1, xd2, xsc, xdc; |
| 1243 |
int ys1, ys2, yd1, yd2, ysc, ydc; |
| 1244 |
Pix32 *srcPtr, *src2Ptr, *destPtr; |
| 1245 |
|
| 1246 |
srcPtr = Blt_ColorImageBits(srcImage); |
| 1247 |
src2Ptr = Blt_ColorImageBits(srcImage2); |
| 1248 |
destPtr = Blt_ColorImageBits(destImage); |
| 1249 |
|
| 1250 |
xsc = (src2.width/2); |
| 1251 |
ysc = (src2.height/2); |
| 1252 |
xdc = (dest.width/2); |
| 1253 |
ydc = (dest.height/2); |
| 1254 |
for (xs1 = xsc-1, xs2=xsc, xd1 = xdc-1, xd2=xd1+1; |
| 1255 |
xd1>=0; xd1--, xs1--, xd2++, xs2++) { |
| 1256 |
if (xs1<0) { xs1 = xsc-1; xs2 = xs1+1; } |
| 1257 |
for (ys1 = ysc-1, ys2 = ysc, yd1 = ydc-1, yd2=yd1+1; |
| 1258 |
yd1>=0; yd1--, ys1--, yd2++, ys2++) { |
| 1259 |
if (ys1<0) { ys1 = ysc-1; ys2 = ys1+1; } |
| 1260 |
|
| 1261 |
#define DDDC_O(ind,ind2) if ( withColor.value == srcPtr[ind].value) destPtr[ind].value = src2Ptr[ind2].value |
| 1262 |
#define DDDC(ind,ind2) PixMerged(srcPtr+(ind), src2Ptr+(ind2), destPtr+(ind), &withColor) |
| 1263 |
|
| 1264 |
DDDC(yd1*dest.width+xd1, ys1*src2.width+xs1); |
| 1265 |
if (ys2>=src2.height) { ys2--; } |
| 1266 |
if (xs2>=src2.width) { xs2--; } |
| 1267 |
if (yd2<dest.height && xd2<dest.width) { |
| 1268 |
DDDC(yd2*dest.width+xd2, ys2*src2.width+xs2); |
| 1269 |
} |
| 1270 |
if (xd2<dest.width) { |
| 1271 |
DDDC(yd1*dest.width+xd2, ys1*src2.width+xs2); |
| 1272 |
} |
| 1273 |
if (yd2<dest.height) { |
| 1274 |
DDDC(yd2*dest.width+xd1, ys2*src2.width+xs1); |
| 1275 |
} |
| 1276 |
} |
| 1277 |
} |
| 1278 |
result = TCL_OK; |
| 1279 |
|
| 1280 |
} |
| 1281 |
if (result == TCL_OK) { |
| 1282 |
Blt_ColorImageToPhoto(destImage, destPhoto); |
| 1283 |
} |
| 1284 |
Blt_FreeColorImage(srcImage); |
| 1285 |
Blt_FreeColorImage(srcImage2); |
| 1286 |
Blt_FreeColorImage(destImage); |
| 1287 |
return result; |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
/*ARGSUSED*/ |
| 1292 |
static int |
| 1293 |
MergeOp(clientData, interp, argc, argv) |
| 1294 |
ClientData clientData; /* Not used. */ |
| 1295 |
Tcl_Interp *interp; |
| 1296 |
int argc; /* Not used. */ |
| 1297 |
char **argv; |
| 1298 |
{ |
| 1299 |
Tk_PhotoHandle srcPhoto, srcPhoto2, destPhoto; |
| 1300 |
Tk_PhotoImageBlock src, src2, dest; |
| 1301 |
Blt_ColorImage srcImage, srcImage2, destImage; |
| 1302 |
double opacity; |
| 1303 |
double opacity2; |
| 1304 |
int result, isWc; |
| 1305 |
Pix32 withColor; |
| 1306 |
|
| 1307 |
isWc = 0; |
| 1308 |
opacity = 0.5; |
| 1309 |
opacity2 = -1; |
| 1310 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 1311 |
if (srcPhoto == NULL) { |
| 1312 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 1313 |
" exist or is not a photo image", (char *)NULL); |
| 1314 |
return TCL_ERROR; |
| 1315 |
} |
| 1316 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 1317 |
if ((src.width <= 1) || (src.height <= 1)) { |
| 1318 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" is empty", |
| 1319 |
(char *)NULL); |
| 1320 |
return TCL_ERROR; |
| 1321 |
} |
| 1322 |
|
| 1323 |
srcPhoto2 = Blt_FindPhoto(interp, argv[3]); |
| 1324 |
if (srcPhoto2 == NULL) { |
| 1325 |
Tcl_AppendResult(interp, "source image \"", argv[3], "\" doesn't", |
| 1326 |
" exist or is not a photo image", (char *)NULL); |
| 1327 |
return TCL_ERROR; |
| 1328 |
} |
| 1329 |
Tk_PhotoGetImage(srcPhoto2, &src2); |
| 1330 |
if ((src2.width <= 1) || (src2.height <= 1)) { |
| 1331 |
Tcl_AppendResult(interp, "source image \"", argv[3], "\" is empty", |
| 1332 |
(char *)NULL); |
| 1333 |
return TCL_ERROR; |
| 1334 |
} |
| 1335 |
if (argc>5) { |
| 1336 |
|
| 1337 |
if (isdigit(argv[5][0]) == 0 && argv[5][0] != '.') { |
| 1338 |
Tk_Window tkwin; |
| 1339 |
char *colStr; |
| 1340 |
|
| 1341 |
tkwin = Tk_MainWindow(interp); |
| 1342 |
isWc = 1; |
| 1343 |
|
| 1344 |
colStr = argv[5]; |
| 1345 |
if (colStr[0] == '!') { |
| 1346 |
colStr++; |
| 1347 |
isWc = 2; |
| 1348 |
} |
| 1349 |
|
| 1350 |
if (GetColorPix32(interp, tkwin, colStr, &withColor) != TCL_OK) { |
| 1351 |
return TCL_ERROR; |
| 1352 |
} |
| 1353 |
if (src.width < 4 || src.height < 4) { |
| 1354 |
Tcl_AppendResult(interp, "src image too small ", 0); |
| 1355 |
return TCL_ERROR; |
| 1356 |
} |
| 1357 |
if (src2.width < 4 || src2.height < 4) { |
| 1358 |
Tcl_AppendResult(interp, "src2 image too small ", 0); |
| 1359 |
return TCL_ERROR; |
| 1360 |
} |
| 1361 |
goto domerge; |
| 1362 |
} |
| 1363 |
|
| 1364 |
if (Tcl_GetDouble(interp, argv[5], &opacity) != TCL_OK) { |
| 1365 |
return TCL_ERROR; |
| 1366 |
} |
| 1367 |
if (opacity < 0.0 || opacity > 1.0) { |
| 1368 |
Tcl_AppendResult(interp, "opacity must be >= 0.0 and <= 1.0: ", argv[5], (char *)NULL); |
| 1369 |
return TCL_ERROR; |
| 1370 |
} |
| 1371 |
} |
| 1372 |
if (argc>6) { |
| 1373 |
if (Tcl_GetDouble(interp, argv[6], &opacity2) != TCL_OK) { |
| 1374 |
return TCL_ERROR; |
| 1375 |
} |
| 1376 |
if (opacity2 < 0.0 || opacity2 > 1.0) { |
| 1377 |
Tcl_AppendResult(interp, "opacity must be >= 0.0 and <= 1.0: ", argv[6], (char *)NULL); |
| 1378 |
return TCL_ERROR; |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
if (isWc == 0 && ((src2.width != src.width) || (src2.height != src.height))) { |
| 1383 |
int sh, sw; |
| 1384 |
sw = (src2.width>src.width?src2.width:src.width); |
| 1385 |
sh = (src2.height>src.height?src2.height:src.height); |
| 1386 |
Tk_PhotoSetSize(srcPhoto2, sw, sh); |
| 1387 |
Tk_PhotoGetImage(srcPhoto2, &src2); |
| 1388 |
if (sw != src.width || sh != src.height) { |
| 1389 |
Tk_PhotoSetSize(srcPhoto, sw, sh); |
| 1390 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 1391 |
} |
| 1392 |
} |
| 1393 |
|
| 1394 |
domerge: |
| 1395 |
destPhoto = Blt_FindPhoto(interp, argv[4]); |
| 1396 |
if (destPhoto == NULL) { |
| 1397 |
Tcl_AppendResult(interp, "destination image \"", argv[4], "\" doesn't", |
| 1398 |
" exist or is not a photo image", (char *)NULL); |
| 1399 |
return TCL_ERROR; |
| 1400 |
} |
| 1401 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 1402 |
|
| 1403 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 1404 |
srcImage2 = Blt_PhotoToColorImage(srcPhoto2); |
| 1405 |
destImage = Blt_PhotoToColorImage(destPhoto); |
| 1406 |
|
| 1407 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 1408 |
if ((dest.width != src.width) || (dest.height != src.height)) { |
| 1409 |
Tk_PhotoSetSize(destPhoto, src.width, src.height); |
| 1410 |
destImage = Blt_PhotoToColorImage(destPhoto); |
| 1411 |
} |
| 1412 |
if (isWc != 1) { |
| 1413 |
result = Blt_MergeColorImage(srcImage, srcImage2, destImage, opacity, opacity2, |
| 1414 |
isWc ? &withColor : NULL); |
| 1415 |
} else { |
| 1416 |
int xs1, xs2, xd1, xd2, xsc, xdc; |
| 1417 |
int ys1, ys2, yd1, yd2, ysc, ydc; |
| 1418 |
Pix32 *srcPtr, *src2Ptr, *destPtr; |
| 1419 |
|
| 1420 |
srcPtr = Blt_ColorImageBits(srcImage); |
| 1421 |
src2Ptr = Blt_ColorImageBits(srcImage2); |
| 1422 |
destPtr = Blt_ColorImageBits(destImage); |
| 1423 |
|
| 1424 |
xsc = (src2.width/2); |
| 1425 |
ysc = (src2.height/2); |
| 1426 |
xdc = (dest.width/2); |
| 1427 |
ydc = (dest.height/2); |
| 1428 |
for (xs1 = xsc-1, xs2=xs1+1, xd1 = xdc-1, xd2=xdc; |
| 1429 |
xd1>=0; xd1--, xs1--, xd2++, xs2++) { |
| 1430 |
if (xs1<0) { xs1 = xsc-1; xs2 = xs1+1; } |
| 1431 |
for (ys1 = ysc-1, ys2 = ys1+1, yd1 = ydc-1, yd2=yd1+1; |
| 1432 |
yd1>=0; yd1--, ys1--, yd2++, ys2++) { |
| 1433 |
if (ys1<0) { ys1 = ysc-1; ys2 = ys1+1; } |
| 1434 |
|
| 1435 |
#define DDC(ind,ind2) destPtr[ind].value = ( withColor.value == srcPtr[ind].value ? src2Ptr[ind2].value : srcPtr[ind].value) |
| 1436 |
|
| 1437 |
DDC(yd1*dest.width+xd1, ys1*src2.width+xs1); |
| 1438 |
//destPtr[yd1*dest.width+xd1].value = srcPtr[ys1*src2.width+xs1].value; |
| 1439 |
if (ys2>=src2.height) { ys2--; } |
| 1440 |
if (xs2>=src2.width) { xs2--; } |
| 1441 |
if (yd2<dest.height && xd2<dest.width) |
| 1442 |
DDC(yd2*dest.width+xd2, ys2*src2.width+xs2); |
| 1443 |
// destPtr[yd2*dest.width+xd2].value = srcPtr[ys2*src2.width+xs2].value; |
| 1444 |
if (xd2<dest.width) |
| 1445 |
DDC(yd1*dest.width+xd2, ys1*src2.width+xs2); |
| 1446 |
//destPtr[yd1*dest.width+xd2].value = srcPtr[ys1*src2.width+xs2].value; |
| 1447 |
if (yd2<dest.height) |
| 1448 |
DDC(yd2*dest.width+xd1, ys2*src2.width+xs1); |
| 1449 |
//destPtr[yd2*dest.width+xd1].value = srcPtr[ys2*src2.width+xs1].value; |
| 1450 |
//if (ya<src.height && ya2<dest.height && xa<src.width && xa2<dest.width) {} |
| 1451 |
} |
| 1452 |
} |
| 1453 |
result = TCL_OK; |
| 1454 |
|
| 1455 |
} |
| 1456 |
if (result == TCL_OK) { |
| 1457 |
Blt_ColorImageToPhoto(destImage, destPhoto); |
| 1458 |
} |
| 1459 |
Blt_FreeColorImage(srcImage); |
| 1460 |
Blt_FreeColorImage(srcImage2); |
| 1461 |
Blt_FreeColorImage(destImage); |
| 1462 |
return result; |
| 1463 |
} |
| 1464 |
|
| 1465 |
|
| 1466 |
/*ARGSUSED*/ |
| 1467 |
static int |
| 1468 |
ReadJPEGOp(clientData, interp, argc, argv) |
| 1469 |
ClientData clientData; /* Not used. */ |
| 1470 |
Tcl_Interp *interp; |
| 1471 |
int argc; /* Not used. */ |
| 1472 |
char **argv; |
| 1473 |
{ |
| 1474 |
#if HAVE_JPEGLIB_H |
| 1475 |
int result; |
| 1476 |
Tcl_DString dString; |
| 1477 |
char *fileName; |
| 1478 |
Tk_PhotoHandle photo; /* The photo image to write into. */ |
| 1479 |
|
| 1480 |
photo = Blt_FindPhoto(interp, argv[3]); |
| 1481 |
if (photo == NULL) { |
| 1482 |
Tcl_AppendResult(interp, "image \"", argv[3], "\" doesn't", |
| 1483 |
" exist or is not a photo image", (char *)NULL); |
| 1484 |
return TCL_ERROR; |
| 1485 |
} |
| 1486 |
Tcl_DStringInit(&dString); |
| 1487 |
fileName = Tcl_TranslateFileName(interp, argv[2], &dString); |
| 1488 |
|
| 1489 |
result = Blt_JPEGToPhoto(interp, fileName, photo); |
| 1490 |
Tcl_DStringInit(&dString); |
| 1491 |
return result; |
| 1492 |
#else |
| 1493 |
Tcl_AppendResult(interp, "JPEG support not compiled", (char *)NULL); |
| 1494 |
return TCL_ERROR; |
| 1495 |
#endif |
| 1496 |
} |
| 1497 |
|
| 1498 |
static double Drand( double val) { |
| 1499 |
#ifdef HAVE_DRAND48 |
| 1500 |
return drand48(); |
| 1501 |
#else |
| 1502 |
return (double)random()/RAND_MAX; |
| 1503 |
#endif |
| 1504 |
} |
| 1505 |
|
| 1506 |
#define SLANTX(x,y) (doslant==0 || slant == 0.0?x:(((int)(x + (y * slant)))%src.width)) |
| 1507 |
#define ARCX(x,y) (doarc==0 || sineVal == 0.0?x:(((int)(x + (src.width*sqrt(0.5*0.5-((double)y/src.height-0.5)*((double)y/src.height-0.5)) * sineVal)))%src.width)) |
| 1508 |
#define SINEX(x,y) (dosine==0 || sineVal == 0.0?x:(((int)(x + (src.width*(*tfunc)(M_PI *y/src.height) * sineVal)))%src.width)) |
| 1509 |
#ifdef HAVE_DRAND48 |
| 1510 |
#define RANDX(c) (dorand==0 || randVal == 0.0?c:(c+(Drand(0) * randVal - (randVal/2.0)))) |
| 1511 |
#else |
| 1512 |
#define RANDX(c) c |
| 1513 |
#endif |
| 1514 |
#define SKEWX(c) ((doskew==0 || skew == 1.0)?c:(ox>=skew?1.0:c/skew)) |
| 1515 |
#define CLAMP(c) ((((c) < 0.0) ? 0.0 : ((c) > 1.0) ? 1.0 : (c))) |
| 1516 |
|
| 1517 |
static int |
| 1518 |
GradientsOp(clientData, interp, argc, argv) |
| 1519 |
ClientData clientData; /* Not used. */ |
| 1520 |
Tcl_Interp *interp; |
| 1521 |
int argc; /* Not used. */ |
| 1522 |
char **argv; |
| 1523 |
{ |
| 1524 |
double range[3]; |
| 1525 |
double left[3]; |
| 1526 |
int x, width; |
| 1527 |
double t; |
| 1528 |
XColor C; |
| 1529 |
XColor *leftPtr, *rightPtr; |
| 1530 |
Tk_Window tkwin; |
| 1531 |
|
| 1532 |
tkwin = Tk_MainWindow(interp); |
| 1533 |
|
| 1534 |
leftPtr = Tk_GetColor(interp, tkwin, Tk_GetUid(argv[2])); |
| 1535 |
if (leftPtr == NULL) { |
| 1536 |
return TCL_ERROR; |
| 1537 |
} |
| 1538 |
rightPtr = Tk_GetColor(interp, tkwin, Tk_GetUid(argv[3])); |
| 1539 |
if (rightPtr == NULL) { |
| 1540 |
return TCL_ERROR; |
| 1541 |
} |
| 1542 |
if (Tcl_GetInt(interp, argv[4], &width) != TCL_OK) { |
| 1543 |
return TCL_ERROR; |
| 1544 |
} |
| 1545 |
if (width<=2) { |
| 1546 |
Tcl_AppendResult(interp, "width must be > 2", 0); |
| 1547 |
return TCL_ERROR; |
| 1548 |
} |
| 1549 |
left[0] = (double)(leftPtr->red >> 8); |
| 1550 |
left[1] = (double)(leftPtr->green >> 8); |
| 1551 |
left[2] = (double)(leftPtr->blue >> 8); |
| 1552 |
range[0] = (double)((rightPtr->red - leftPtr->red) / 257.0); |
| 1553 |
range[1] = (double)((rightPtr->green - leftPtr->green) / 257.0); |
| 1554 |
range[2] = (double)((rightPtr->blue - leftPtr->blue) / 257.0); |
| 1555 |
|
| 1556 |
for (x = 0; x < width; x++) { |
| 1557 |
char cbuf[100]; |
| 1558 |
t = ((double)( sin(M_PI_2 * (double)x / (double)width))); |
| 1559 |
t = CLAMP(t); |
| 1560 |
C.red = (unsigned short)(left[0] + t * range[0]); |
| 1561 |
C.green = (unsigned short)(left[1] + t * range[1]); |
| 1562 |
C.blue = (unsigned short)(left[2] + t * range[2]); |
| 1563 |
sprintf(cbuf, "#%02x%02x%02x", C.red, C.green, C.blue); |
| 1564 |
if (x) { Tcl_AppendResult(interp, " ", 0); } |
| 1565 |
Tcl_AppendResult(interp, cbuf, 0); |
| 1566 |
} |
| 1567 |
return TCL_OK; |
| 1568 |
} |
| 1569 |
|
| 1570 |
int |
| 1571 |
Blt_GetGradient(Tcl_Interp *interp, Tk_Window tkwin, Gradient *gradPtr) { |
| 1572 |
XColor *leftPtr, *rightPtr; |
| 1573 |
double range[3]; |
| 1574 |
double left[3]; |
| 1575 |
int x, width; |
| 1576 |
double t; |
| 1577 |
XColor **destPtr, C; |
| 1578 |
|
| 1579 |
leftPtr = gradPtr->color; |
| 1580 |
rightPtr = gradPtr->color2; |
| 1581 |
left[0] = (double)(leftPtr->red >> 8); |
| 1582 |
left[1] = (double)(leftPtr->green >> 8); |
| 1583 |
left[2] = (double)(leftPtr->blue >> 8); |
| 1584 |
range[0] = (double)((rightPtr->red - leftPtr->red) / 257.0); |
| 1585 |
range[1] = (double)((rightPtr->green - leftPtr->green) / 257.0); |
| 1586 |
range[2] = (double)((rightPtr->blue - leftPtr->blue) / 257.0); |
| 1587 |
|
| 1588 |
width = gradPtr->width; |
| 1589 |
if (gradPtr->grads) { |
| 1590 |
Blt_FreeGradient(gradPtr); |
| 1591 |
} |
| 1592 |
destPtr = gradPtr->grads = (XColor**)Blt_Calloc(width+1, sizeof(XColor*)); |
| 1593 |
for (x = 0; x < width; x++) { |
| 1594 |
char cbuf[100]; |
| 1595 |
t = ((double)( sin(M_PI_2 * (double)x / (double)width))); |
| 1596 |
t = CLAMP(t); |
| 1597 |
C.red = (unsigned short)(left[0] + t * range[0]); |
| 1598 |
C.green = (unsigned short)(left[1] + t * range[1]); |
| 1599 |
C.blue = (unsigned short)(left[2] + t * range[2]); |
| 1600 |
/**destPtr = Tk_GetColorByValue(tkwin, &C); */ |
| 1601 |
sprintf(cbuf, "#%02x%02x%02x", C.red, C.green, C.blue); |
| 1602 |
*destPtr = Tk_GetColor(interp, tkwin, Tk_GetUid(cbuf)); |
| 1603 |
if (*destPtr == NULL) break; |
| 1604 |
destPtr++; |
| 1605 |
} |
| 1606 |
gradPtr->origColor = gradPtr->color; |
| 1607 |
gradPtr->origColor2 = gradPtr->color2; |
| 1608 |
gradPtr->origWidth = gradPtr->width; |
| 1609 |
return TCL_OK; |
| 1610 |
} |
| 1611 |
|
| 1612 |
int |
| 1613 |
Blt_FreeGradient(Gradient *gradPtr) { |
| 1614 |
XColor **destPtr; |
| 1615 |
if (gradPtr->grads) { |
| 1616 |
destPtr = gradPtr->grads; |
| 1617 |
while (*destPtr) { |
| 1618 |
Tk_FreeColor(*destPtr); |
| 1619 |
destPtr++; |
| 1620 |
} |
| 1621 |
Blt_Free( gradPtr->grads); |
| 1622 |
} |
| 1623 |
gradPtr->grads = NULL; |
| 1624 |
return TCL_OK; |
| 1625 |
|
| 1626 |
} |
| 1627 |
|
| 1628 |
/*ARGSUSED*/ |
| 1629 |
static int |
| 1630 |
GradientOp(clientData, interp, argc, argv) |
| 1631 |
ClientData clientData; /* Not used. */ |
| 1632 |
Tcl_Interp *interp; |
| 1633 |
int argc; /* Not used. */ |
| 1634 |
char **argv; |
| 1635 |
{ |
| 1636 |
Tk_PhotoHandle photo; |
| 1637 |
Tk_PhotoImageBlock src; |
| 1638 |
XColor *leftPtr, *rightPtr; |
| 1639 |
Tk_Window tkwin; |
| 1640 |
double range[3]; |
| 1641 |
double left[3]; |
| 1642 |
Pix32 *destPtr; |
| 1643 |
Blt_ColorImage destImage; |
| 1644 |
double skew, randVal, slant, sineVal; |
| 1645 |
int dorand, doslant, doskew, doarc, dosine, gtype, result, ox; |
| 1646 |
int nWidth = 0, nHeight = 0; |
| 1647 |
Tcl_Obj *objPtr; |
| 1648 |
static char *types[] = { "linear", "radial", "sine", "halfsine", "rectangular", "split", "blank", 0 }; |
| 1649 |
enum ITypeIdx { |
| 1650 |
ILinearIdx, IRadialIdx, ISineIdx, IHalfSineIdx, IRectIdx, ISplitIdx, |
| 1651 |
IBlankIdx |
| 1652 |
}; |
| 1653 |
double (*tfunc)(double); |
| 1654 |
int alpha; |
| 1655 |
unsigned char aVal; |
| 1656 |
|
| 1657 |
aVal = 255; |
| 1658 |
tfunc = &sin; |
| 1659 |
dorand = 0; |
| 1660 |
doskew = 0; |
| 1661 |
doslant = 0; |
| 1662 |
dosine = 0; |
| 1663 |
doarc = 0; |
| 1664 |
skew = 1.0; |
| 1665 |
gtype = ISineIdx; |
| 1666 |
tkwin = Tk_MainWindow(interp); |
| 1667 |
photo = Blt_FindPhoto(interp, argv[2]); |
| 1668 |
if (photo == NULL) { |
| 1669 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 1670 |
" exist or is not a photo image", (char *)NULL); |
| 1671 |
return TCL_ERROR; |
| 1672 |
} |
| 1673 |
Tk_PhotoGetImage(photo, &src); |
| 1674 |
leftPtr = Tk_GetColor(interp, tkwin, Tk_GetUid(argv[3])); |
| 1675 |
if (leftPtr == NULL) { |
| 1676 |
return TCL_ERROR; |
| 1677 |
} |
| 1678 |
rightPtr = Tk_GetColor(interp, tkwin, Tk_GetUid(argv[4])); |
| 1679 |
if (rightPtr == NULL) { |
| 1680 |
return TCL_ERROR; |
| 1681 |
} |
| 1682 |
while (argc > 5) { |
| 1683 |
if (argc < 7) { |
| 1684 |
Tcl_AppendResult(interp, "expected argument", argv[5], (char *)NULL); |
| 1685 |
return TCL_ERROR; |
| 1686 |
} |
| 1687 |
if (!strcmp("-alpha", argv[5])) { |
| 1688 |
if (Tcl_GetInt(interp, argv[6], &alpha) != TCL_OK) { |
| 1689 |
return TCL_ERROR; |
| 1690 |
} |
| 1691 |
if (alpha<=0 || alpha > 255) { |
| 1692 |
Tcl_AppendResult(interp, "alpha must be > 0 and <= 255", argv[6], |
| 1693 |
(char *)NULL); |
| 1694 |
return TCL_ERROR; |
| 1695 |
} |
| 1696 |
aVal = (unsigned char) alpha; |
| 1697 |
|
| 1698 |
} else if (!strcmp("-type", argv[5])) { |
| 1699 |
objPtr = Tcl_NewStringObj(argv[6],-1); |
| 1700 |
Tcl_IncrRefCount(objPtr); |
| 1701 |
result = Tcl_GetIndexFromObj(interp, objPtr, types, "type", 0, |
| 1702 |
>ype); |
| 1703 |
Tcl_DecrRefCount(objPtr); |
| 1704 |
if (result != TCL_OK) { |
| 1705 |
return TCL_ERROR; |
| 1706 |
} |
| 1707 |
|
| 1708 |
} else if (!strcmp("-skew", argv[5])) { |
| 1709 |
doskew = 1; |
| 1710 |
if (Tcl_GetDouble(interp, argv[6], &skew) != TCL_OK) { |
| 1711 |
return TCL_ERROR; |
| 1712 |
} |
| 1713 |
if (skew<0.0 || skew>1.0) { |
| 1714 |
Tcl_AppendResult(interp, "skew must be >=0 && <=1.0: ", argv[6], |
| 1715 |
(char *)NULL); |
| 1716 |
return TCL_ERROR; |
| 1717 |
} |
| 1718 |
} else if (!strcmp("-width", argv[5])) { |
| 1719 |
if (Tk_GetPixels(interp, tkwin, argv[6], &nWidth) != TCL_OK) { |
| 1720 |
return TCL_ERROR; |
| 1721 |
} |
| 1722 |
if (nWidth<1 || nWidth>5000) { |
| 1723 |
Tcl_AppendResult(interp, "width must be >=1 && <=5000: ", argv[6], |
| 1724 |
(char *)NULL); |
| 1725 |
return TCL_ERROR; |
| 1726 |
} |
| 1727 |
} else if (!strcmp("-height", argv[5])) { |
| 1728 |
if (Tk_GetPixels(interp, tkwin, argv[6], &nHeight) != TCL_OK) { |
| 1729 |
return TCL_ERROR; |
| 1730 |
} |
| 1731 |
if (nHeight<1 || nHeight>5000) { |
| 1732 |
Tcl_AppendResult(interp, "width must be >=1 && <=5000: ", argv[6], |
| 1733 |
(char *)NULL); |
| 1734 |
return TCL_ERROR; |
| 1735 |
} |
| 1736 |
} else if (!strcmp("-rand", argv[5])) { |
| 1737 |
dorand = 1; |
| 1738 |
if (Tcl_GetDouble(interp, argv[6], &randVal) != TCL_OK) { |
| 1739 |
return TCL_ERROR; |
| 1740 |
} |
| 1741 |
if (randVal<0.0 || randVal>0.1) { |
| 1742 |
Tcl_AppendResult(interp, "randVal must be >= 0.0 && <= 0.1: ", argv[6], |
| 1743 |
(char *)NULL); |
| 1744 |
return TCL_ERROR; |
| 1745 |
} |
| 1746 |
} else if (!strcmp("-slant", argv[5])) { |
| 1747 |
doslant = 1; |
| 1748 |
if (Tcl_GetDouble(interp, argv[6], &slant) != TCL_OK) { |
| 1749 |
return TCL_ERROR; |
| 1750 |
} |
| 1751 |
if (slant<-1000.0 || slant>1000.0) { |
| 1752 |
Tcl_AppendResult(interp, "slant must be >= -1000.0 && <= 1000.0: ", argv[6], |
| 1753 |
(char *)NULL); |
| 1754 |
return TCL_ERROR; |
| 1755 |
} |
| 1756 |
} else if (!strcmp("-mathval", argv[5])) { |
| 1757 |
dosine = 1; |
| 1758 |
if (Tcl_GetDouble(interp, argv[6], &sineVal) != TCL_OK) { |
| 1759 |
return TCL_ERROR; |
| 1760 |
} |
| 1761 |
if (sineVal<-1000.0 || sineVal>1000.0) { |
| 1762 |
Tcl_AppendResult(interp, "mathval must be >= -1000.0 && <= 1000.0: ", argv[6], |
| 1763 |
(char *)NULL); |
| 1764 |
return TCL_ERROR; |
| 1765 |
} |
| 1766 |
} else if (!strcmp("-mathfunc", argv[5])) { |
| 1767 |
if (!strcmp("circle", argv[6])) { |
| 1768 |
dosine = 0; |
| 1769 |
doarc = 1; |
| 1770 |
} else if (!strcmp("sin", argv[6])) { |
| 1771 |
tfunc = &sin; |
| 1772 |
} else if (!strcmp("cos", argv[6])) { |
| 1773 |
tfunc = &cos; |
| 1774 |
} else if (!strcmp("atan", argv[6])) { |
| 1775 |
tfunc = &atan; |
| 1776 |
} else if (!strcmp("acos", argv[6])) { |
| 1777 |
tfunc = &acos; |
| 1778 |
} else if (!strcmp("asin", argv[6])) { |
| 1779 |
tfunc = &asin; |
| 1780 |
} else if (!strcmp("rand", argv[6])) { |
| 1781 |
tfunc = &Drand; |
| 1782 |
} else if (!strcmp("cosh", argv[6])) { |
| 1783 |
tfunc = &cosh; |
| 1784 |
} else if (!strcmp("sinh", argv[6])) { |
| 1785 |
tfunc = &sinh; |
| 1786 |
} else if (!strcmp("tan", argv[6])) { |
| 1787 |
tfunc = &tan; |
| 1788 |
} else if (!strcmp("tanh", argv[6])) { |
| 1789 |
tfunc = &tanh; |
| 1790 |
} else if (!strcmp("log", argv[6])) { |
| 1791 |
tfunc = &log; |
| 1792 |
} else if (!strcmp("log10", argv[6])) { |
| 1793 |
tfunc = &log10; |
| 1794 |
} else if (!strcmp("exp", argv[6])) { |
| 1795 |
tfunc = &exp; |
| 1796 |
} else if (!strcmp("sqrt", argv[6])) { |
| 1797 |
tfunc = &sqrt; |
| 1798 |
} else { |
| 1799 |
Tcl_AppendResult(interp, "mathfunc ", argv[6], " not one of: ", |
| 1800 |
"sin cos tan sinh cosh tanh asin acos atan log log10 exp sqrt rand circle", (char *)NULL); |
| 1801 |
return TCL_ERROR; |
| 1802 |
} |
| 1803 |
} else { |
| 1804 |
Tcl_AppendResult(interp, "argument \"", argv[5], "\" not one of: -type -skew -rand -slant -curve -circle", |
| 1805 |
(char *)NULL); |
| 1806 |
return TCL_ERROR; |
| 1807 |
} |
| 1808 |
argc -= 2; |
| 1809 |
argv += 2; |
| 1810 |
} |
| 1811 |
if ((nWidth>0 || nHeight>0) && ((nWidth != src.width) || (nHeight != src.height))) { |
| 1812 |
if (nWidth<=0) { nWidth = src.width; } |
| 1813 |
if (nHeight<=0) { nHeight = src.height; } |
| 1814 |
Tk_PhotoSetSize(photo, nWidth, nHeight); |
| 1815 |
Tk_PhotoGetImage(photo, &src); |
| 1816 |
} |
| 1817 |
left[0] = (double)(leftPtr->red >> 8); |
| 1818 |
left[1] = (double)(leftPtr->green >> 8); |
| 1819 |
left[2] = (double)(leftPtr->blue >> 8); |
| 1820 |
range[0] = (double)((rightPtr->red - leftPtr->red) / 257.0); |
| 1821 |
range[1] = (double)((rightPtr->green - leftPtr->green) / 257.0); |
| 1822 |
range[2] = (double)((rightPtr->blue - leftPtr->blue) / 257.0); |
| 1823 |
|
| 1824 |
destImage = Blt_CreateColorImage(src.width, src.height); |
| 1825 |
destPtr = Blt_ColorImageBits(destImage); |
| 1826 |
|
| 1827 |
if (gtype == ILinearIdx) { |
| 1828 |
register int x, y, sx; |
| 1829 |
double t; |
| 1830 |
|
| 1831 |
for (y = 0; y < src.height; y++) { |
| 1832 |
for (x = 0; x < src.width; x++) { |
| 1833 |
ox = (double)x / src.width; |
| 1834 |
sx = SLANTX(x,y); |
| 1835 |
sx = ARCX(sx,y); |
| 1836 |
sx = SINEX(sx,y); |
| 1837 |
t = (double)sx / src.width; |
| 1838 |
t = SKEWX(t); |
| 1839 |
t = RANDX(t); |
| 1840 |
t = CLAMP(t); |
| 1841 |
destPtr->Red = (unsigned char)(left[0] + t * range[0]); |
| 1842 |
destPtr->Green = (unsigned char)(left[1] + t * range[1]); |
| 1843 |
destPtr->Blue = (unsigned char)(left[2] + t * range[2]); |
| 1844 |
destPtr->Alpha = aVal; |
| 1845 |
destPtr++; |
| 1846 |
} |
| 1847 |
} |
| 1848 |
} else if (gtype == ISineIdx) { |
| 1849 |
register int x, y, sx; |
| 1850 |
double t; |
| 1851 |
|
| 1852 |
for (y = 0; y < src.height; y++) { |
| 1853 |
for (x = 0; x < src.width; x++) { |
| 1854 |
ox = (double)x / src.width; |
| 1855 |
sx = SLANTX(x,y); |
| 1856 |
sx = ARCX(sx,y); |
| 1857 |
sx = SINEX(sx,y); |
| 1858 |
t = ((double)(sin(2*M_PI_2 * (double)sx / (double)src.width))); |
| 1859 |
t = SKEWX(t); |
| 1860 |
t = RANDX(t); |
| 1861 |
t = CLAMP(t); |
| 1862 |
destPtr->Red = (unsigned char)(left[0] + t * range[0]); |
| 1863 |
destPtr->Green = (unsigned char)(left[1] + t * range[1]); |
| 1864 |
destPtr->Blue = (unsigned char)(left[2] + t * range[2]); |
| 1865 |
destPtr->Alpha = aVal; |
| 1866 |
destPtr++; |
| 1867 |
} |
| 1868 |
} |
| 1869 |
} else if (gtype == IHalfSineIdx) { |
| 1870 |
register int x, y, sx; |
| 1871 |
double t; |
| 1872 |
|
| 1873 |
for (y = 0; y < src.height; y++) { |
| 1874 |
for (x = 0; x < src.width; x++) { |
| 1875 |
ox = (double)x / src.width; |
| 1876 |
sx = SLANTX(x,y); |
| 1877 |
sx = ARCX(sx,y); |
| 1878 |
sx = SINEX(sx,y); |
| 1879 |
t = ((double)( sin(M_PI_2 * (double)sx / (double)src.width))); |
| 1880 |
t = SKEWX(t); |
| 1881 |
t = RANDX(t); |
| 1882 |
t = CLAMP(t); |
| 1883 |
destPtr->Red = (unsigned char)(left[0] + t * range[0]); |
| 1884 |
destPtr->Green = (unsigned char)(left[1] + t * range[1]); |
| 1885 |
destPtr->Blue = (unsigned char)(left[2] + t * range[2]); |
| 1886 |
destPtr->Alpha = aVal; |
| 1887 |
destPtr++; |
| 1888 |
} |
| 1889 |
} |
| 1890 |
} else if (gtype == IRadialIdx) { |
| 1891 |
register int x, y, sx; |
| 1892 |
register double dx, dy; |
| 1893 |
double dy2; |
| 1894 |
double t; |
| 1895 |
double midX, midY; |
| 1896 |
|
| 1897 |
midX = midY = 0.5; |
| 1898 |
for (y = 0; y < src.height; y++) { |
| 1899 |
dy = (y / (double)src.height) - midY; |
| 1900 |
dy2 = dy * dy; |
| 1901 |
for (x = 0; x < src.width; x++) { |
| 1902 |
ox = (double)x / src.width; |
| 1903 |
sx = SLANTX(x,y); |
| 1904 |
sx = ARCX(sx,y); |
| 1905 |
sx = SINEX(sx,y); |
| 1906 |
dx = (sx / (double)src.width) - midX; |
| 1907 |
t = 1.0 - (double)sqrt(dx * dx + dy2); |
| 1908 |
t = SKEWX(t); |
| 1909 |
t = RANDX(t); |
| 1910 |
t = CLAMP(t); |
| 1911 |
destPtr->Red = (unsigned char)(left[0] + t * range[0]); |
| 1912 |
destPtr->Green = (unsigned char)(left[1] + t * range[1]); |
| 1913 |
destPtr->Blue = (unsigned char)(left[2] + t * range[2]); |
| 1914 |
destPtr->Alpha = aVal; |
| 1915 |
destPtr++; |
| 1916 |
} |
| 1917 |
} |
| 1918 |
|
| 1919 |
} else if (gtype == IRectIdx) { |
| 1920 |
register int x, y, sx; |
| 1921 |
register double dx, dy; |
| 1922 |
double t, px, py; |
| 1923 |
double midX, midY; |
| 1924 |
double cosTheta, sinTheta; |
| 1925 |
double angle; |
| 1926 |
|
| 1927 |
angle = M_PI_2 * -0.3; |
| 1928 |
cosTheta = cos(angle); |
| 1929 |
sinTheta = sin(angle); |
| 1930 |
|
| 1931 |
midX = 0.5, midY = 0.5; |
| 1932 |
for (y = 0; y < src.height; y++) { |
| 1933 |
dy = (y / (double)src.height) - midY; |
| 1934 |
for (x = 0; x < src.width; x++) { |
| 1935 |
ox = (double)x / src.width; |
| 1936 |
sx = SLANTX(x,y); |
| 1937 |
sx = ARCX(sx,y); |
| 1938 |
sx = SINEX(sx,y); |
| 1939 |
dx = (sx / (double)src.width) - midX; |
| 1940 |
px = dx * cosTheta - dy * sinTheta; |
| 1941 |
py = dx * sinTheta + dy * cosTheta; |
| 1942 |
t = FABS(px) + FABS(py); |
| 1943 |
t = SKEWX(t); |
| 1944 |
t = RANDX(t); |
| 1945 |
t = CLAMP(t); |
| 1946 |
destPtr->Red = (unsigned char)(left[0] + t * range[0]); |
| 1947 |
destPtr->Green = (unsigned char)(left[1] + t * range[1]); |
| 1948 |
destPtr->Blue = (unsigned char)(left[2] + t * range[2]); |
| 1949 |
destPtr->Alpha = aVal; |
| 1950 |
destPtr++; |
| 1951 |
} |
| 1952 |
} |
| 1953 |
} else if (gtype == ISplitIdx) { |
| 1954 |
register int x, y, sx, midx; |
| 1955 |
double t; |
| 1956 |
|
| 1957 |
midx = src.width/2; |
| 1958 |
for (y = 0; y < src.height; y++) { |
| 1959 |
for (x = 0; x < src.width; x++) { |
| 1960 |
ox = (double)x / src.width; |
| 1961 |
sx = SLANTX(x,y); |
| 1962 |
sx = ARCX(sx,y); |
| 1963 |
sx = SINEX(sx,y); |
| 1964 |
if (x<midx) { |
| 1965 |
sx += (0.1 * src.width); |
| 1966 |
} else { |
| 1967 |
sx -= (0.1 * src.width); |
| 1968 |
} |
| 1969 |
t = (double)sx / src.width; |
| 1970 |
t = SKEWX(t); |
| 1971 |
t = RANDX(t); |
| 1972 |
t = CLAMP(t); |
| 1973 |
destPtr->Red = (unsigned char)(left[0] + t * range[0]); |
| 1974 |
destPtr->Green = (unsigned char)(left[1] + t * range[1]); |
| 1975 |
destPtr->Blue = (unsigned char)(left[2] + t * range[2]); |
| 1976 |
destPtr->Alpha = aVal; |
| 1977 |
destPtr++; |
| 1978 |
} |
| 1979 |
} |
| 1980 |
} else if (gtype == IBlankIdx) { |
| 1981 |
register int x, y; |
| 1982 |
|
| 1983 |
for (y = 0; y < src.height; y++) { |
| 1984 |
for (x = 0; x < src.width; x++) { |
| 1985 |
destPtr->Red = (unsigned char)0xFF; |
| 1986 |
destPtr->Green = (unsigned char)0xFF; |
| 1987 |
destPtr->Blue = (unsigned char)0xFF; |
| 1988 |
destPtr->Alpha = aVal; |
| 1989 |
destPtr++; |
| 1990 |
} |
| 1991 |
} |
| 1992 |
} |
| 1993 |
Blt_ColorImageToPhoto(destImage, photo); |
| 1994 |
Blt_FreeColorImage(destImage); |
| 1995 |
return TCL_OK; |
| 1996 |
} |
| 1997 |
|
| 1998 |
/*ARGSUSED*/ |
| 1999 |
static int |
| 2000 |
ResampleOp(clientData, interp, argc, argv) |
| 2001 |
ClientData clientData; /* Not used. */ |
| 2002 |
Tcl_Interp *interp; |
| 2003 |
int argc; /* Not used. */ |
| 2004 |
char **argv; |
| 2005 |
{ |
| 2006 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 2007 |
Tk_PhotoImageBlock src, dest; |
| 2008 |
ResampleFilter *filterPtr, *vertFilterPtr, *horzFilterPtr; |
| 2009 |
char *filterName; |
| 2010 |
|
| 2011 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 2012 |
if (srcPhoto == NULL) { |
| 2013 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 2014 |
" exist or is not a photo image", (char *)NULL); |
| 2015 |
return TCL_ERROR; |
| 2016 |
} |
| 2017 |
destPhoto = Blt_FindPhoto(interp, argv[3]); |
| 2018 |
if (destPhoto == NULL) { |
| 2019 |
Tcl_AppendResult(interp, "destination image \"", argv[3], "\" doesn't", |
| 2020 |
" exist or is not a photo image", (char *)NULL); |
| 2021 |
return TCL_ERROR; |
| 2022 |
} |
| 2023 |
filterName = (argc > 4) ? argv[4] : "none"; |
| 2024 |
if (Blt_GetResampleFilter(interp, filterName, &filterPtr) != TCL_OK) { |
| 2025 |
return TCL_ERROR; |
| 2026 |
} |
| 2027 |
vertFilterPtr = horzFilterPtr = filterPtr; |
| 2028 |
if ((filterPtr != NULL) && (argc > 5)) { |
| 2029 |
if (Blt_GetResampleFilter(interp, argv[5], &filterPtr) != TCL_OK) { |
| 2030 |
return TCL_ERROR; |
| 2031 |
} |
| 2032 |
vertFilterPtr = filterPtr; |
| 2033 |
} |
| 2034 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 2035 |
if ((src.width <= 1) || (src.height <= 1)) { |
| 2036 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" is empty", |
| 2037 |
(char *)NULL); |
| 2038 |
return TCL_ERROR; |
| 2039 |
} |
| 2040 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 2041 |
if ((dest.width <= 1) || (dest.height <= 1)) { |
| 2042 |
Tk_PhotoSetSize(destPhoto, src.width, src.height); |
| 2043 |
goto copyImage; |
| 2044 |
} |
| 2045 |
if ((src.width == dest.width) && (src.height == dest.height)) { |
| 2046 |
copyImage: |
| 2047 |
/* Source and destination image sizes are the same. Don't |
| 2048 |
* resample. Simply make copy of image */ |
| 2049 |
dest.width = src.width; |
| 2050 |
dest.height = src.height; |
| 2051 |
dest.pixelPtr = src.pixelPtr; |
| 2052 |
dest.pixelSize = src.pixelSize; |
| 2053 |
dest.pitch = src.pitch; |
| 2054 |
dest.offset[0] = src.offset[0]; |
| 2055 |
dest.offset[1] = src.offset[1]; |
| 2056 |
dest.offset[2] = src.offset[2]; |
| 2057 |
Tk_PhotoPutBlock(destPhoto, &dest, 0, 0, dest.width, dest.height); |
| 2058 |
return TCL_OK; |
| 2059 |
} |
| 2060 |
if (filterPtr == NULL) { |
| 2061 |
Blt_ResizePhoto(srcPhoto, 0, 0, src.width, src.height, destPhoto); |
| 2062 |
} else { |
| 2063 |
Blt_ResamplePhoto(srcPhoto, 0, 0, src.width, src.height, destPhoto, |
| 2064 |
horzFilterPtr, vertFilterPtr); |
| 2065 |
} |
| 2066 |
return TCL_OK; |
| 2067 |
} |
| 2068 |
|
| 2069 |
/*ARGSUSED*/ |
| 2070 |
static int |
| 2071 |
BlurOp(clientData, interp, argc, argv) |
| 2072 |
ClientData clientData; /* Not used. */ |
| 2073 |
Tcl_Interp *interp; |
| 2074 |
int argc; /* Not used. */ |
| 2075 |
char **argv; |
| 2076 |
{ |
| 2077 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 2078 |
Tk_PhotoImageBlock src, dest; |
| 2079 |
double radius; |
| 2080 |
|
| 2081 |
radius = 3; |
| 2082 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 2083 |
if (srcPhoto == NULL) { |
| 2084 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 2085 |
" exist or is not a photo image", (char *)NULL); |
| 2086 |
return TCL_ERROR; |
| 2087 |
} |
| 2088 |
destPhoto = Blt_FindPhoto(interp, argv[3]); |
| 2089 |
if (destPhoto == NULL) { |
| 2090 |
Tcl_AppendResult(interp, "destination image \"", argv[3], "\" doesn't", |
| 2091 |
" exist or is not a photo image", (char *)NULL); |
| 2092 |
return TCL_ERROR; |
| 2093 |
} |
| 2094 |
if (argc > 4) { |
| 2095 |
if (Tcl_GetDouble(interp, argv[4], &radius) != TCL_OK) { |
| 2096 |
return TCL_ERROR; |
| 2097 |
} |
| 2098 |
} |
| 2099 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 2100 |
if ((src.width <= 1) || (src.height <= 1)) { |
| 2101 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" is empty", |
| 2102 |
(char *)NULL); |
| 2103 |
return TCL_ERROR; |
| 2104 |
} |
| 2105 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 2106 |
Tk_PhotoSetSize(destPhoto, src.width, src.height); |
| 2107 |
return Blt_BlurColorImage(srcPhoto, destPhoto, (int)(radius + 0.5) ); |
| 2108 |
} |
| 2109 |
|
| 2110 |
/*ARGSUSED*/ |
| 2111 |
static int |
| 2112 |
RotateOp(clientData, interp, argc, argv) |
| 2113 |
ClientData clientData; /* Not used. */ |
| 2114 |
Tcl_Interp *interp; |
| 2115 |
int argc; /* Not used. */ |
| 2116 |
char **argv; |
| 2117 |
{ |
| 2118 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 2119 |
Blt_ColorImage srcImage, destImage; |
| 2120 |
double theta; |
| 2121 |
|
| 2122 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 2123 |
if (srcPhoto == NULL) { |
| 2124 |
Tcl_AppendResult(interp, "image \"", argv[2], "\" doesn't", |
| 2125 |
" exist or is not a photo image", (char *)NULL); |
| 2126 |
return TCL_ERROR; |
| 2127 |
} |
| 2128 |
destPhoto = Blt_FindPhoto(interp, argv[3]); |
| 2129 |
if (destPhoto == NULL) { |
| 2130 |
Tcl_AppendResult(interp, "destination image \"", argv[3], "\" doesn't", |
| 2131 |
" exist or is not a photo image", (char *)NULL); |
| 2132 |
return TCL_ERROR; |
| 2133 |
} |
| 2134 |
if (Tcl_ExprDouble(interp, argv[4], &theta) != TCL_OK) { |
| 2135 |
return TCL_ERROR; |
| 2136 |
} |
| 2137 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 2138 |
destImage = Blt_RotateColorImage(srcImage, theta); |
| 2139 |
|
| 2140 |
Blt_ColorImageToPhoto(destImage, destPhoto); |
| 2141 |
Blt_FreeColorImage(srcImage); |
| 2142 |
Blt_FreeColorImage(destImage); |
| 2143 |
return TCL_OK; |
| 2144 |
} |
| 2145 |
|
| 2146 |
/*ARGSUSED*/ |
| 2147 |
int |
| 2148 |
Blt_ImageMirror(Tcl_Interp *interp, char *srcImg, char *dstImg, int flip, int flags) { |
| 2149 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 2150 |
Blt_ColorImage srcImage, destImage; |
| 2151 |
int x, y, x1, x2, y2; |
| 2152 |
Tk_PhotoImageBlock src, dest; |
| 2153 |
Pix32 *destPtr, *srcPtr; |
| 2154 |
|
| 2155 |
srcPhoto = Blt_FindPhoto(interp, srcImg); |
| 2156 |
if (srcPhoto == NULL) { |
| 2157 |
Tcl_AppendResult(interp, "image \"", srcImg, "\" doesn't", |
| 2158 |
" exist or is not a photo image", (char *)NULL); |
| 2159 |
return TCL_ERROR; |
| 2160 |
} |
| 2161 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 2162 |
destPhoto = Blt_FindPhoto(interp, dstImg); |
| 2163 |
if (destPhoto == NULL) { |
| 2164 |
Tcl_AppendResult(interp, "destination image \"", dstImg, "\" doesn't", |
| 2165 |
" exist or is not a photo image", (char *)NULL); |
| 2166 |
return TCL_ERROR; |
| 2167 |
} |
| 2168 |
|
| 2169 |
|
| 2170 |
if ((flip == MIRROR_TILE || flip == MIRROR_INNER || flip == MIRROR_OUTER ) && srcPhoto == destPhoto) { |
| 2171 |
Tcl_AppendResult(interp, "image must be different", (char *)NULL); |
| 2172 |
return TCL_ERROR; |
| 2173 |
} |
| 2174 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 2175 |
if (src.width <= 0 || src.height <= 0) { |
| 2176 |
Tcl_AppendResult(interp, "src image empty ", 0); |
| 2177 |
return TCL_ERROR; |
| 2178 |
} |
| 2179 |
|
| 2180 |
if (flip == MIRROR_TILE) { |
| 2181 |
if ((dest.width != (src.width *2)) || (dest.height <= (src.height*2))) { |
| 2182 |
Tk_PhotoSetSize(destPhoto, 2*src.width, 2*src.height); |
| 2183 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 2184 |
} |
| 2185 |
} else if (flip != MIRROR_OUTER && flip != MIRROR_INNER) { |
| 2186 |
if ((dest.width != src.width) || (dest.height <= src.height)) { |
| 2187 |
Tk_PhotoSetSize(destPhoto, src.width, src.height); |
| 2188 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 2189 |
} |
| 2190 |
} |
| 2191 |
|
| 2192 |
srcImage = Blt_PhotoToColorImage(srcPhoto); |
| 2193 |
destImage = Blt_PhotoToColorImage(destPhoto); |
| 2194 |
destPtr = Blt_ColorImageBits(destImage); |
| 2195 |
srcPtr = Blt_ColorImageBits(srcImage); |
| 2196 |
|
| 2197 |
if (flip == MIRROR_INNER) { |
| 2198 |
/* Center */ |
| 2199 |
int xs1, xs2, xd1, xd2, xsc, xdc; |
| 2200 |
int ys1, ys2, yd1, yd2, ysc, ydc; |
| 2201 |
|
| 2202 |
if (src.width < 4 || src.height < 4) { |
| 2203 |
Tcl_AppendResult(interp, "src image too small ", 0); |
| 2204 |
return TCL_ERROR; |
| 2205 |
} |
| 2206 |
if (dest.width < 4 || dest.height < 4) { |
| 2207 |
Tcl_AppendResult(interp, "dest image too small ", 0); |
| 2208 |
return TCL_ERROR; |
| 2209 |
} |
| 2210 |
xsc = (src.width/2); |
| 2211 |
ysc = (src.height/2); |
| 2212 |
xdc = (dest.width/2); |
| 2213 |
ydc = (dest.height/2); |
| 2214 |
for (xs1 = xsc-1, xs2=xs1+1, xd1 = xdc-1, xd2=xd1+1; |
| 2215 |
xd1>=0; xd1--, xs1--, xd2++, xs2++) { |
| 2216 |
if (xs1<0) { xs1 = xsc-1; xs2 = xs1+1; } |
| 2217 |
for (ys1 = ysc-1, ys2 = ys1+1, yd1 = ydc-1, yd2=yd1+1; |
| 2218 |
yd1>=0; yd1--, ys1--, yd2++, ys2++) { |
| 2219 |
if (ys1<0) { ys1 = ysc-1; ys2 = ys1+1; } |
| 2220 |
destPtr[yd1*dest.width+xd1].value = srcPtr[ys1*src.width+xs1].value; |
| 2221 |
if (ys2>=src.height) { ys2--; } |
| 2222 |
if (xs2>=src.width) { xs2--; } |
| 2223 |
if (yd2<dest.height && xd2<dest.width) |
| 2224 |
destPtr[yd2*dest.width+xd2].value = srcPtr[ys2*src.width+xs2].value; |
| 2225 |
if (xd2<dest.width) |
| 2226 |
destPtr[yd1*dest.width+xd2].value = srcPtr[ys1*src.width+xs2].value; |
| 2227 |
if (yd2<dest.height) |
| 2228 |
destPtr[yd2*dest.width+xd1].value = srcPtr[ys2*src.width+xs1].value; |
| 2229 |
//if (ya<src.height && ya2<dest.height && xa<src.width && xa2<dest.width) {} |
| 2230 |
} |
| 2231 |
} |
| 2232 |
} |
| 2233 |
if (flip == MIRROR_X) { |
| 2234 |
/* Flip X */ |
| 2235 |
for (y = 0; y < src.height; y++) { |
| 2236 |
for (x = 0, x1 = src.width*y, x2 = src.width*(y+1)-1; |
| 2237 |
x < src.width; |
| 2238 |
x++, x1++, x2--) { |
| 2239 |
destPtr[x2].value = srcPtr[x1].value; |
| 2240 |
} |
| 2241 |
} |
| 2242 |
} |
| 2243 |
if (flip == MIRROR_Y) { |
| 2244 |
/* Flip Y */ |
| 2245 |
for (x = 0; x < src.width; x++) { |
| 2246 |
for (y = 0, y2 = src.height-1; y < src.height; y++, y2--) { |
| 2247 |
destPtr[y2*dest.width+x].value = srcPtr[y*src.width+x].value; |
| 2248 |
} |
| 2249 |
} |
| 2250 |
} |
| 2251 |
if (flip == MIRROR_XY) { |
| 2252 |
/* Flip X and Y */ |
| 2253 |
for (x = 0, x2 = src.width-1; x < src.width; x++, x2--) { |
| 2254 |
for (y = 0, y2 = src.height-1; y < src.height; y++, y2--) { |
| 2255 |
destPtr[y2*dest.width+x2].value = srcPtr[y*src.width+x].value; |
| 2256 |
} |
| 2257 |
} |
| 2258 |
} |
| 2259 |
|
| 2260 |
if (flip == MIRROR_TILE) { |
| 2261 |
for (y = 0; y < src.height; y++) { |
| 2262 |
for (x = 0; x < src.width; x++) { |
| 2263 |
destPtr[y*dest.width+x].value = srcPtr[y*src.width+x].value; |
| 2264 |
} |
| 2265 |
} |
| 2266 |
/* Flip X */ |
| 2267 |
for (y = 0; y < src.height; y++) { |
| 2268 |
for (x = 0, x1 = src.width*y, x2 = 2*src.width*(y+1)-1; |
| 2269 |
x < src.width; |
| 2270 |
x++, x1++, x2--) { |
| 2271 |
destPtr[x2].value = srcPtr[x1].value; |
| 2272 |
} |
| 2273 |
} |
| 2274 |
/* Flip Y */ |
| 2275 |
for (x = 0; x < src.width; x++) { |
| 2276 |
for (y = 0, y2 = 2*src.height-1; y < src.height; y++, y2--) { |
| 2277 |
destPtr[y2*dest.width+x].value = srcPtr[y*src.width+x].value; |
| 2278 |
} |
| 2279 |
} |
| 2280 |
/* Flip X and Y */ |
| 2281 |
for (x = 0, x2 = 2*src.width-1; x < src.width; x++, x2--) { |
| 2282 |
for (y = 0, y2 = 2*src.height-1; y < src.height; y++, y2--) { |
| 2283 |
destPtr[y2*dest.width+x2].value = srcPtr[y*src.width+x].value; |
| 2284 |
} |
| 2285 |
} |
| 2286 |
} |
| 2287 |
|
| 2288 |
if (flip == MIRROR_OUTER) { |
| 2289 |
int split = (flags&1); |
| 2290 |
int sx1, sx2, sy1, sy2, |
| 2291 |
smx = (src.width/2), smy=(src.height/2), |
| 2292 |
dmx = (dest.width/2), dmy=(dest.height/2); |
| 2293 |
|
| 2294 |
/* Initialize whole background to center pixel. */ |
| 2295 |
int sind = (src.height/2)*src.width+src.width/2; |
| 2296 |
for (x = 0; x < dest.width; x++) { |
| 2297 |
for (y = 0; y < dest.height; y++) { |
| 2298 |
destPtr[y*dest.width+x].value = |
| 2299 |
srcPtr[sind].value; |
| 2300 |
} |
| 2301 |
} |
| 2302 |
|
| 2303 |
/* Copy top & bottom pixels. */ |
| 2304 |
for (y = 0, y2 = dest.height-1, sy1 = 0, sy2 = src.height-1; |
| 2305 |
y < dmy; |
| 2306 |
y++, y2--, sy1++, sy2--) { |
| 2307 |
if (sy1 >= smy) { if (split) { sy1--; sy2++; } else { sy1 = sy2 = smy; }} |
| 2308 |
for (x = 0, x2 = dest.width-1, sx1 = 0, sx2 = src.width-1; |
| 2309 |
x < dmx; x++, x2--, sx1++, sx2--) { |
| 2310 |
if (sx1 >= smx) { if (split) { sx1--; sx2++; } else { sx1 = sx2 = smx; }} |
| 2311 |
/* Top - left & right */ |
| 2312 |
destPtr[y*dest.width+x].value = srcPtr[sy1*src.width+sx1].value; |
| 2313 |
destPtr[y*dest.width+x2].value = srcPtr[sy1*src.width+sx2].value; |
| 2314 |
/* Bottom - left & right */ |
| 2315 |
destPtr[y2*dest.width+x].value = srcPtr[sy2*src.width+sx1].value; |
| 2316 |
destPtr[y2*dest.width+x2].value = srcPtr[sy2*src.width+sx2].value; |
| 2317 |
} |
| 2318 |
} |
| 2319 |
/* Copy left & right pixels. */ |
| 2320 |
for (x = 0, x2 = dest.width-1, sx1 = 0, sx2 = src.width-1; |
| 2321 |
x < smx && x < dmx; |
| 2322 |
x++, x2--, sx1++, sx2--) { |
| 2323 |
if (sx1 >= smx) { if (split) { sx1--; sx2++; } else { sx1 = sx2 = smx; }} |
| 2324 |
for (y = 0, y2 = dest.height-1, sy1 = 0, sy2 = src.height-1; |
| 2325 |
y < dmy; y++, y2--, sy1++, sy2--) { |
| 2326 |
if (sy1 >= smy) { if (split) { sy1--; sy2++; } else { sy1 = sy2 = smy; }} |
| 2327 |
/* Left - top & bottom */ |
| 2328 |
destPtr[y*dest.width+x].value = srcPtr[sy1*src.width+sx1].value; |
| 2329 |
destPtr[y*dest.width+x2].value = srcPtr[sy1*src.width+sx2].value; |
| 2330 |
/* Right - top & bottom */ |
| 2331 |
destPtr[y2*dest.width+x].value = srcPtr[sy2*src.width+sx1].value; |
| 2332 |
destPtr[y2*dest.width+x2].value = srcPtr[sy2*src.width+sx2].value; |
| 2333 |
} |
| 2334 |
} |
| 2335 |
} |
| 2336 |
|
| 2337 |
Blt_ColorImageToPhoto(destImage, destPhoto); |
| 2338 |
Blt_FreeColorImage(srcImage); |
| 2339 |
Blt_FreeColorImage(destImage); |
| 2340 |
return TCL_OK; |
| 2341 |
} |
| 2342 |
|
| 2343 |
|
| 2344 |
/*ARGSUSED*/ |
| 2345 |
static int |
| 2346 |
MirrorOp(clientData, interp, argc, argv) |
| 2347 |
ClientData clientData; /* Not used. */ |
| 2348 |
Tcl_Interp *interp; |
| 2349 |
int argc; /* Not used. */ |
| 2350 |
char **argv; |
| 2351 |
{ |
| 2352 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 2353 |
int flip, halo; |
| 2354 |
|
| 2355 |
flip = 3; |
| 2356 |
halo = 0; |
| 2357 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 2358 |
if (srcPhoto == NULL) { |
| 2359 |
Tcl_AppendResult(interp, "image \"", argv[2], "\" doesn't", |
| 2360 |
" exist or is not a photo image", (char *)NULL); |
| 2361 |
return TCL_ERROR; |
| 2362 |
} |
| 2363 |
destPhoto = Blt_FindPhoto(interp, argv[3]); |
| 2364 |
if (destPhoto == NULL) { |
| 2365 |
Tcl_AppendResult(interp, "destination image \"", argv[3], "\" doesn't", |
| 2366 |
" exist or is not a photo image", (char *)NULL); |
| 2367 |
return TCL_ERROR; |
| 2368 |
} |
| 2369 |
if (argc>4) { |
| 2370 |
if (!strcmp(argv[4],"x")) { |
| 2371 |
flip = MIRROR_X; /* 1 */ |
| 2372 |
} else if (!strcmp(argv[4],"y")) { |
| 2373 |
flip = MIRROR_Y; /* 2 */ |
| 2374 |
} else if (!strcmp(argv[4],"xy")) { |
| 2375 |
flip = MIRROR_XY; /* 3 */ |
| 2376 |
} else if (!strcmp(argv[4],"tile")) { |
| 2377 |
flip = MIRROR_TILE; /* 4 */ |
| 2378 |
} else if (!strcmp(argv[4],"outer")) { |
| 2379 |
flip = MIRROR_OUTER; /* 5 */ |
| 2380 |
} else if (!strcmp(argv[4],"inner")) { |
| 2381 |
flip = MIRROR_INNER; /* 6 */ |
| 2382 |
} else { |
| 2383 |
Tcl_AppendResult(interp, "direction ", argv[4], |
| 2384 |
" must be \"x\", \"y\", \"xy\",\"tile\", \"inner\", or \"outer\"", (char *)NULL); |
| 2385 |
return TCL_ERROR; |
| 2386 |
} |
| 2387 |
} |
| 2388 |
if (argc>5) { |
| 2389 |
if (flip != MIRROR_OUTER) { |
| 2390 |
Tcl_AppendResult(interp, "halo is for outer only", 0); |
| 2391 |
return TCL_ERROR; |
| 2392 |
} |
| 2393 |
if (Tcl_GetInt(interp, argv[5], &halo) != TCL_OK) { |
| 2394 |
return TCL_ERROR; |
| 2395 |
} |
| 2396 |
} |
| 2397 |
return Blt_ImageMirror(interp, argv[2], argv[3], flip, halo); |
| 2398 |
} |
| 2399 |
|
| 2400 |
|
| 2401 |
/* |
| 2402 |
* -------------------------------------------------------------------------- |
| 2403 |
* |
| 2404 |
* SnapOp -- |
| 2405 |
* |
| 2406 |
* Snaps a picture of a window and stores it in a designated |
| 2407 |
* photo image. The window must be completely visible or |
| 2408 |
* the snap will fail. |
| 2409 |
* |
| 2410 |
* Results: |
| 2411 |
* Returns a standard Tcl result. interp->result contains |
| 2412 |
* the list of the graph coordinates. If an error occurred |
| 2413 |
* while parsing the window positions, TCL_ERROR is returned, |
| 2414 |
* then interp->result will contain an error message. |
| 2415 |
* |
| 2416 |
* ------------------------------------------------------------------------- |
| 2417 |
*/ |
| 2418 |
/*ARGSUSED*/ |
| 2419 |
static int |
| 2420 |
SnapOp(clientData, interp, argc, argv) |
| 2421 |
ClientData clientData; |
| 2422 |
Tcl_Interp *interp; |
| 2423 |
int argc; /* Not used. */ |
| 2424 |
char **argv; |
| 2425 |
{ |
| 2426 |
Tk_Window tkwin; |
| 2427 |
int width, height, destWidth, destHeight; |
| 2428 |
Window window; |
| 2429 |
|
| 2430 |
tkwin = Tk_MainWindow(interp); |
| 2431 |
window = StringToWindow(interp, argv[2]); |
| 2432 |
if (window == None) { |
| 2433 |
return TCL_ERROR; |
| 2434 |
} |
| 2435 |
if (GetWindowSize(interp, window, &width, &height) != TCL_OK) { |
| 2436 |
Tcl_AppendResult(interp, "can't get window geometry of \"", argv[2], |
| 2437 |
"\"", (char *)NULL); |
| 2438 |
return TCL_ERROR; |
| 2439 |
} |
| 2440 |
destWidth = width, destHeight = height; |
| 2441 |
if ((argc > 4) && (Blt_GetPixels(interp, tkwin, argv[4], PIXELS_POSITIVE, |
| 2442 |
&destWidth) != TCL_OK)) { |
| 2443 |
return TCL_ERROR; |
| 2444 |
} |
| 2445 |
if ((argc > 5) && (Blt_GetPixels(interp, tkwin, argv[5], PIXELS_POSITIVE, |
| 2446 |
&destHeight) != TCL_OK)) { |
| 2447 |
return TCL_ERROR; |
| 2448 |
} |
| 2449 |
return Blt_SnapPhoto(interp, tkwin, window, 0, 0, width, height, destWidth, |
| 2450 |
destHeight, argv[3], GAMMA); |
| 2451 |
} |
| 2452 |
|
| 2453 |
/*ARGSUSED*/ |
| 2454 |
static int |
| 2455 |
SubsampleOp(clientData, interp, argc, argv) |
| 2456 |
ClientData clientData; /* Main window of the interpreter. */ |
| 2457 |
Tcl_Interp *interp; |
| 2458 |
int argc; /* Not used. */ |
| 2459 |
char **argv; |
| 2460 |
{ |
| 2461 |
Tk_Window tkwin; |
| 2462 |
Tk_PhotoHandle srcPhoto, destPhoto; |
| 2463 |
Tk_PhotoImageBlock src, dest; |
| 2464 |
ResampleFilter *filterPtr, *vertFilterPtr, *horzFilterPtr; |
| 2465 |
char *filterName; |
| 2466 |
int flag; |
| 2467 |
int x, y; |
| 2468 |
int width, height; |
| 2469 |
|
| 2470 |
srcPhoto = Blt_FindPhoto(interp, argv[2]); |
| 2471 |
if (srcPhoto == NULL) { |
| 2472 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" doesn't", |
| 2473 |
" exist or is not a photo image", (char *)NULL); |
| 2474 |
return TCL_ERROR; |
| 2475 |
} |
| 2476 |
destPhoto = Blt_FindPhoto(interp, argv[3]); |
| 2477 |
if (destPhoto == NULL) { |
| 2478 |
Tcl_AppendResult(interp, "destination image \"", argv[3], "\" doesn't", |
| 2479 |
" exist or is not a photo image", (char *)NULL); |
| 2480 |
return TCL_ERROR; |
| 2481 |
} |
| 2482 |
tkwin = (Tk_Window)clientData; |
| 2483 |
flag = PIXELS_NONNEGATIVE; |
| 2484 |
if (Blt_GetPixels(interp, tkwin, argv[4], flag, &x) != TCL_OK) { |
| 2485 |
return TCL_ERROR; |
| 2486 |
} |
| 2487 |
if (Blt_GetPixels(interp, tkwin, argv[5], flag, &y) != TCL_OK) { |
| 2488 |
return TCL_ERROR; |
| 2489 |
} |
| 2490 |
flag = PIXELS_POSITIVE; |
| 2491 |
if (Blt_GetPixels(interp, tkwin, argv[6], flag, &width) != TCL_OK) { |
| 2492 |
return TCL_ERROR; |
| 2493 |
} |
| 2494 |
if (Blt_GetPixels(interp, tkwin, argv[7], flag, &height) != TCL_OK) { |
| 2495 |
return TCL_ERROR; |
| 2496 |
} |
| 2497 |
filterName = (argc > 8) ? argv[8] : "none"; |
| 2498 |
if (Blt_GetResampleFilter(interp, filterName, &filterPtr) != TCL_OK) { |
| 2499 |
return TCL_ERROR; |
| 2500 |
} |
| 2501 |
vertFilterPtr = horzFilterPtr = filterPtr; |
| 2502 |
if ((filterPtr != NULL) && (argc > 9)) { |
| 2503 |
if (Blt_GetResampleFilter(interp, argv[9], &filterPtr) != TCL_OK) { |
| 2504 |
return TCL_ERROR; |
| 2505 |
} |
| 2506 |
vertFilterPtr = filterPtr; |
| 2507 |
} |
| 2508 |
Tk_PhotoGetImage(srcPhoto, &src); |
| 2509 |
Tk_PhotoGetImage(destPhoto, &dest); |
| 2510 |
if ((src.width <= 1) || (src.height <= 1)) { |
| 2511 |
Tcl_AppendResult(interp, "source image \"", argv[2], "\" is empty", |
| 2512 |
(char *)NULL); |
| 2513 |
return TCL_ERROR; |
| 2514 |
} |
| 2515 |
if (((x + width) > src.width) || ((y + height) > src.height)) { |
| 2516 |
Tcl_AppendResult(interp, "nonsensical dimensions for subregion: x=", |
| 2517 |
argv[4], " y=", argv[5], " width=", argv[6], " height=", |
| 2518 |
argv[7], (char *)NULL); |
| 2519 |
return TCL_ERROR; |
| 2520 |
} |
| 2521 |
if ((dest.width <= 1) || (dest.height <= 1)) { |
| 2522 |
Tk_PhotoSetSize(destPhoto, width, height); |
| 2523 |
} |
| 2524 |
if (filterPtr == NULL) { |
| 2525 |
Blt_ResizePhoto(srcPhoto, x, y, width, height, destPhoto); |
| 2526 |
} else { |
| 2527 |
Blt_ResamplePhoto(srcPhoto, x, y, width, height, destPhoto, |
| 2528 |
horzFilterPtr, vertFilterPtr); |
| 2529 |
} |
| 2530 |
return TCL_OK; |
| 2531 |
} |
| 2532 |
|
| 2533 |
static Blt_OpSpec imageOps[] = |
| 2534 |
{ |
| 2535 |
{"alpha", 2, (Blt_Op)AlphaOp, 6, 10, "?-shift? srcPhoto destPhoto Color ?alpha? ?withalpha?",}, |
| 2536 |
{"blur", 2, (Blt_Op)BlurOp, 5, 6, "srcPhoto destPhoto ?radius?",}, |
| 2537 |
{"colors", 3, (Blt_Op)ColorsOp, 4, 0, "?-alpha? ?-count? ?-from x1 y1 x2 y2? srcPhoto",}, |
| 2538 |
{"convolve", 3, (Blt_Op)ConvolveOp, 6, 6, "srcPhoto destPhoto filter",}, |
| 2539 |
{"gradient", 1, (Blt_Op)GradientOp, 6, 0, "photo left right ?-type lines|normal|radial|rectangular|linear|sine|halfsine? ?-skew val? ?-slant val? ?-rand val? ?-mathval val? ?-mathfunc circle|sin|cos|tan|asin|acos|atan|sinh|cosh|tanh|exp|sqrt|log|log10|rand?",}, |
| 2540 |
{"merge", 2, (Blt_Op)MergeOp, 6, 7, "srcPhoto1 srcPhoto2 destPhoto ?opacity? ?opacity2?",}, |
| 2541 |
{"mirror", 3, (Blt_Op)MirrorOp, 5, 7, "srcPhoto destPhoto ?x|y|xy|tile|outer|innert? ?halo?",}, |
| 2542 |
{"quantize", 2, (Blt_Op)QuantizeOp, 4, 5, "srcPhoto destPhoto ?nColors?",}, |
| 2543 |
{"readjpeg", 3, (Blt_Op)ReadJPEGOp, 5, 5, "fileName photoName",}, |
| 2544 |
{"recolor", 3, (Blt_Op)RecolorOp, 7, 8, "srcPhoto destPhoto oldColor newColor ?alpha?",}, |
| 2545 |
{"resample", 3, (Blt_Op)ResampleOp, 5, 7, |
| 2546 |
"srcPhoto destPhoto ?horzFilter vertFilter?",}, |
| 2547 |
{"rotate", 2, (Blt_Op)RotateOp, 6, 6, "srcPhoto destPhoto angle",}, |
| 2548 |
{"subsample", 2, (Blt_Op)SubsampleOp, 9, 11, |
| 2549 |
"srcPhoto destPhoto x y width height ?horzFilter? ?vertFilter?",}, |
| 2550 |
{"trans", 2, (Blt_Op)TransOp, 6, 7, "image x y ?alpha?",}, |
| 2551 |
}; |
| 2552 |
|
| 2553 |
static int nImageOps = sizeof(imageOps) / sizeof(Blt_OpSpec); |
| 2554 |
|
| 2555 |
/* ARGSUSED */ |
| 2556 |
static int |
| 2557 |
ImageOp(clientData, interp, argc, argv) |
| 2558 |
ClientData clientData; /* Main window of interpreter. */ |
| 2559 |
Tcl_Interp *interp; /* Current interpreter. */ |
| 2560 |
int argc; /* Number of arguments. */ |
| 2561 |
char **argv; /* Argument strings. */ |
| 2562 |
{ |
| 2563 |
Blt_Op proc; |
| 2564 |
int result; |
| 2565 |
|
| 2566 |
proc = Blt_GetOp(interp, nImageOps, imageOps, BLT_OP_ARG2, argc, argv, 0); |
| 2567 |
if (proc == NULL) { |
| 2568 |
return TCL_ERROR; |
| 2569 |
} |
| 2570 |
clientData = (ClientData)Tk_MainWindow(interp); |
| 2571 |
result = (*proc) (clientData, interp, argc - 1, argv + 1); |
| 2572 |
return result; |
| 2573 |
} |
| 2574 |
|
| 2575 |
static Blt_OpSpec winOps[] = |
| 2576 |
{ |
| 2577 |
{"changes", 3, (Blt_Op)ChangesOp, 3, 3, "window",}, |
| 2578 |
#ifndef WIN32 |
| 2579 |
{"colormap", 3, (Blt_Op)ColormapOp, 3, 3, "window",}, |
| 2580 |
#endif |
| 2581 |
{"gradients", 1, (Blt_Op)GradientsOp, 5, 5, "color1 color2 width",}, |
| 2582 |
{"image", 1, (Blt_Op)ImageOp, 2, 0, "args",}, |
| 2583 |
{"lower", 1, (Blt_Op)LowerOp, 2, 0, "window ?window?...",}, |
| 2584 |
{"map", 2, (Blt_Op)MapOp, 2, 0, "window ?window?...",}, |
| 2585 |
{"move", 2, (Blt_Op)MoveOp, 5, 5, "window x y",}, |
| 2586 |
{"parent", 3, (Blt_Op)ParentOp, 3, 3, "window",}, |
| 2587 |
{"query", 3, (Blt_Op)QueryOp, 2, 2, "",}, |
| 2588 |
{"raise", 2, (Blt_Op)RaiseOp, 2, 0, "window ?window?...",}, |
| 2589 |
#ifdef notdef |
| 2590 |
{"reparent", 3, (Blt_Op)ReparentOp, 3, 4, "window ?parent?",}, |
| 2591 |
#endif |
| 2592 |
{"snap", 2, (Blt_Op)SnapOp, 4, 8, "window photoName ?width height?",}, |
| 2593 |
{"unmap", 1, (Blt_Op)UnmapOp, 2, 0, "window ?window?...",}, |
| 2594 |
{"warpto", 1, (Blt_Op)WarpToOp, 2, 5, "?window?",}, |
| 2595 |
}; |
| 2596 |
|
| 2597 |
static int nWinOps = sizeof(winOps) / sizeof(Blt_OpSpec); |
| 2598 |
|
| 2599 |
/* ARGSUSED */ |
| 2600 |
static int |
| 2601 |
WinopCmd(clientData, interp, argc, argv) |
| 2602 |
ClientData clientData; /* Main window of interpreter. */ |
| 2603 |
Tcl_Interp *interp; /* Current interpreter. */ |
| 2604 |
int argc; /* Number of arguments. */ |
| 2605 |
char **argv; /* Argument strings. */ |
| 2606 |
{ |
| 2607 |
Blt_Op proc; |
| 2608 |
int result; |
| 2609 |
|
| 2610 |
proc = Blt_GetOp(interp, nWinOps, winOps, BLT_OP_ARG1, argc, argv, 0); |
| 2611 |
if (proc == NULL) { |
| 2612 |
return TCL_ERROR; |
| 2613 |
} |
| 2614 |
clientData = (ClientData)Tk_MainWindow(interp); |
| 2615 |
result = (*proc) (clientData, interp, argc, argv); |
| 2616 |
return result; |
| 2617 |
} |
| 2618 |
|
| 2619 |
int |
| 2620 |
Blt_WinopInit(interp) |
| 2621 |
Tcl_Interp *interp; |
| 2622 |
{ |
| 2623 |
static Blt_CmdSpec cmdSpec = {"winop", WinopCmd,}; |
| 2624 |
|
| 2625 |
if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) { |
| 2626 |
return TCL_ERROR; |
| 2627 |
} |
| 2628 |
return TCL_OK; |
| 2629 |
} |
| 2630 |
|
| 2631 |
#endif /* NO_WINOP */ |