| 1 |
/* |
| 2 |
Copyright (c) 1995-2004 Nick Ing-Simmons. All rights reserved. |
| 3 |
This program is free software; you can redistribute it and/or |
| 4 |
modify it under the same terms as Perl itself. |
| 5 |
*/ |
| 6 |
#define PERL_NO_GET_CONTEXT |
| 7 |
#include <EXTERN.h> |
| 8 |
#include <perl.h> |
| 9 |
#include <XSUB.h> |
| 10 |
|
| 11 |
#include <tkGlue.def> |
| 12 |
|
| 13 |
#include <pTk/tkPort.h> |
| 14 |
#include <pTk/tkInt.h> |
| 15 |
#include <tkGlue.h> |
| 16 |
#include <tkGlue.m> |
| 17 |
#include <pTk/tkImgPhoto.h> |
| 18 |
#include <pTk/tkImgPhoto.m> |
| 19 |
#include "pTk/imgInt.h" |
| 20 |
#include "pTk/imgInt.m" |
| 21 |
#include <pTk/tkVMacro.h> |
| 22 |
|
| 23 |
#undef memcpy |
| 24 |
|
| 25 |
/* |
| 26 |
* The format record for the Window file format: |
| 27 |
*/ |
| 28 |
|
| 29 |
static int FileMatchWindow _ANSI_ARGS_((Tcl_Channel chan, Tcl_Obj * fileName, |
| 30 |
Tcl_Obj * formatString, int *widthPtr, int *heightPtr, Tcl_Interp *interp)); |
| 31 |
static int FileReadWindow _ANSI_ARGS_((Tcl_Interp *interp, |
| 32 |
Tcl_Channel chan, Tcl_Obj * fileName, Tcl_Obj * formatString, |
| 33 |
Tk_PhotoHandle imageHandle, int destX, int destY, |
| 34 |
int width, int height, int srcX, int srcY)); |
| 35 |
static int StringMatchWindow _ANSI_ARGS_((Tcl_Obj *dataObj, |
| 36 |
Tcl_Obj * formatString, int *widthPtr, int *heightPtr, Tcl_Interp *interp)); |
| 37 |
static int StringReadWindow _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Obj *dataObj, |
| 38 |
Tcl_Obj * formatString, Tk_PhotoHandle imageHandle, |
| 39 |
int destX, int destY, int width, int height, |
| 40 |
int srcX, int srcY)); |
| 41 |
|
| 42 |
Tk_PhotoImageFormat tkImgFmtWindow = { |
| 43 |
"Window", /* name */ |
| 44 |
FileMatchWindow, /* fileMatchProc */ |
| 45 |
StringMatchWindow, /* stringMatchProc */ |
| 46 |
FileReadWindow, /* fileReadProc */ |
| 47 |
StringReadWindow, /* stringReadProc */ |
| 48 |
NULL, /* fileWriteProc */ |
| 49 |
NULL, /* stringWriteProc */ |
| 50 |
}; |
| 51 |
|
| 52 |
static int |
| 53 |
FileMatchWindow(chan, fileName, formatString, widthPtr, heightPtr, interp) |
| 54 |
Tcl_Channel chan; /* The image file, open for reading. */ |
| 55 |
Tcl_Obj * fileName; /* The name of the image file. */ |
| 56 |
Tcl_Obj * formatString; /* User-specified format string, or NULL. */ |
| 57 |
int *widthPtr, *heightPtr; /* The dimensions of the image are |
| 58 |
* returned here if the file is a valid |
| 59 |
* raw Window file. */ |
| 60 |
Tcl_Interp *interp; |
| 61 |
{ |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
static int |
| 67 |
FileReadWindow(interp, chan, fileName, formatString, imageHandle, destX, destY, |
| 68 |
width, height, srcX, srcY) |
| 69 |
Tcl_Interp *interp; /* Interpreter to use for reporting errors. */ |
| 70 |
Tcl_Channel chan; /* The image file, open for reading. */ |
| 71 |
Tcl_Obj * fileName; /* The name of the image file. */ |
| 72 |
Tcl_Obj * formatString; /* User-specified format string, or NULL. */ |
| 73 |
Tk_PhotoHandle imageHandle; /* The photo image to write into. */ |
| 74 |
int destX, destY; /* Coordinates of top-left pixel in |
| 75 |
* photo image to be written to. */ |
| 76 |
int width, height; /* Dimensions of block of photo image to |
| 77 |
* be written to. */ |
| 78 |
int srcX, srcY; /* Coordinates of top-left pixel to be used |
| 79 |
* in image being read. */ |
| 80 |
{ |
| 81 |
return TCL_ERROR; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
static int |
| 86 |
StringMatchWindow(dataObj, formatString, widthPtr, heightPtr, interp) |
| 87 |
Tcl_Obj *dataObj; /* the object containing the image data */ |
| 88 |
Tcl_Obj * formatString; /* the image format string */ |
| 89 |
int *widthPtr; /* where to put the string width */ |
| 90 |
int *heightPtr; /* where to put the string height */ |
| 91 |
Tcl_Interp *interp; |
| 92 |
{ |
| 93 |
long val = 0; |
| 94 |
if (Tcl_GetLongFromObj(interp, dataObj, &val) == TCL_OK) |
| 95 |
{ |
| 96 |
Tk_Window tkwin = Tk_MainWindow(interp); |
| 97 |
Display *dpy = Tk_Display(tkwin); |
| 98 |
Window win = val; |
| 99 |
XWindowAttributes attr; |
| 100 |
XGetWindowAttributes(dpy, win, &attr); |
| 101 |
*widthPtr = attr.width; |
| 102 |
*heightPtr = attr.height; |
| 103 |
return 1; |
| 104 |
} |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static int |
| 109 |
StringReadWindow(interp,dataObj,formatString,imageHandle, |
| 110 |
destX, destY, width, height, srcX, srcY) |
| 111 |
Tcl_Interp *interp; /* interpreter for reporting errors in */ |
| 112 |
Tcl_Obj *dataObj; /* object containing the image */ |
| 113 |
Tcl_Obj * formatString; /* format string if any */ |
| 114 |
Tk_PhotoHandle imageHandle; /* the image to write this data into */ |
| 115 |
int destX, destY; /* The rectangular region of the */ |
| 116 |
int width, height; /* image to copy */ |
| 117 |
int srcX, srcY; |
| 118 |
{ |
| 119 |
long val = 0; |
| 120 |
if (Tcl_GetLongFromObj(interp, dataObj, &val) == TCL_OK) |
| 121 |
{ |
| 122 |
int x; |
| 123 |
int y; |
| 124 |
unsigned char *p; |
| 125 |
Tk_PhotoImageBlock block; |
| 126 |
Tk_Window tkwin = Tk_MainWindow(interp); |
| 127 |
Display *dpy = Tk_Display(tkwin); |
| 128 |
Window win = val; |
| 129 |
XWindowAttributes attr; |
| 130 |
XImage *img; |
| 131 |
XColor color; |
| 132 |
Tcl_HashTable ctable; |
| 133 |
XGetWindowAttributes(dpy, win, &attr); |
| 134 |
Tcl_InitHashTable(&ctable, TCL_ONE_WORD_KEYS); |
| 135 |
if (srcX+width > attr.width) |
| 136 |
{ |
| 137 |
width = attr.width - srcX; |
| 138 |
} |
| 139 |
if (srcY+height > attr.height) |
| 140 |
{ |
| 141 |
height = attr.height - srcY; |
| 142 |
} |
| 143 |
if (width <= 0 || height <= 0) |
| 144 |
{ |
| 145 |
return TCL_ERROR; |
| 146 |
} |
| 147 |
img = XGetImage(dpy, win, srcX, srcY, width, height, |
| 148 |
(unsigned long) -1, XYPixmap); |
| 149 |
|
| 150 |
Tk_PhotoGetImage(imageHandle, &block); |
| 151 |
block.offset[3] = (block.pixelSize > 3) ? 3 : 0; |
| 152 |
block.width = width; |
| 153 |
block.pitch = block.pixelSize * width; |
| 154 |
block.height = height; |
| 155 |
block.pixelPtr = (unsigned char *) |
| 156 |
ckalloc((unsigned) block.pixelSize * width * height); |
| 157 |
p = block.pixelPtr; |
| 158 |
for (y = 0; y < height; y++) |
| 159 |
{ |
| 160 |
for (x = 0; x < width; x++) |
| 161 |
{ |
| 162 |
unsigned char *p = block.pixelPtr+(y*block.pitch)+(x*block.pixelSize); |
| 163 |
Tcl_HashEntry *he; |
| 164 |
int new = 0; |
| 165 |
ClientData cd = 0; |
| 166 |
color.pixel = (*img->f.get_pixel)(img, srcX+x, srcY+y); |
| 167 |
he = Tcl_CreateHashEntry(&ctable,(char *) color.pixel, &new); |
| 168 |
if (new) |
| 169 |
{ |
| 170 |
XQueryColors(dpy, attr.colormap, &color, 1); |
| 171 |
p[0] = color.red >> 8; |
| 172 |
p[1] = color.green >> 8; |
| 173 |
p[2] = color.blue >> 8; |
| 174 |
if (block.pixelSize > 3) |
| 175 |
p[3] = 255; |
| 176 |
Copy(p,&cd,block.pixelSize,unsigned char); |
| 177 |
Tcl_SetHashValue(he, cd); |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
cd = Tcl_GetHashValue(he); |
| 182 |
Copy(&cd, p, block.pixelSize,unsigned char); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
Tk_PhotoExpand(imageHandle, destX + width, destY + height); |
| 187 |
Tk_PhotoPutBlock(imageHandle, &block, destX, destY, width, height, TK_PHOTO_COMPOSITE_SET); |
| 188 |
Tcl_DeleteHashTable(&ctable); |
| 189 |
(*img->f.destroy_image)(img); |
| 190 |
ckfree((char *) block.pixelPtr); |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
croak("Cannot get window from %"SVf,dataObj); |
| 195 |
} |
| 196 |
return TCL_OK; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
DECLARE_VTABLES; |
| 201 |
DECLARE_PHOTO; |
| 202 |
|
| 203 |
MODULE = Tk::WinPhoto PACKAGE = Tk::WinPhoto |
| 204 |
|
| 205 |
PROTOTYPES: DISABLE |
| 206 |
|
| 207 |
BOOT: |
| 208 |
{ |
| 209 |
IMPORT_VTABLES; |
| 210 |
IMPORT_PHOTO; |
| 211 |
Tk_CreatePhotoImageFormat(&tkImgFmtWindow); |
| 212 |
} |
| 213 |
|