Fredrik Lundh is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againpil-2009-raclette / Tk / tkImaging.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | /*
* The Python Imaging Library.
* $Id$
*
* TK interface for Python Imaging objects
*
* Copies (parts of) a named display memory to a photo image object.
* Also contains code to create an display memory. Under Tk, a
* display memory is simply an "L" or "RGB" image memory that is
* allocated in a single block.
*
* To use this module, import the _imagingtk module (ImageTk does
* this for you).
*
* If you're using Python in an embedded context, you can add the
* following lines to your Tcl_AppInit function (in tkappinit.c)
* instead. Put them after the calls to Tcl_Init and Tk_Init:
*
* {
* extern void TkImaging_Init(Tcl_Interp* interp);
* TkImaging_Init(interp);
* }
*
* This registers a Tcl command called "PyImagingPhoto", which is used
* to communicate between PIL and Tk's PhotoImage handler.
*
* Compile and link tkImaging.c with tkappinit.c and _tkinter (see the
* Setup file for details on how to use tkappinit.c). Note that
* _tkinter.c must be compiled with WITH_APPINIT.
*
* History:
* 1995-09-12 fl Created
* 1996-04-08 fl Ready for release
* 1997-05-09 fl Use command instead of image type
* 2001-03-18 fl Initialize alpha layer pointer (struct changed in 8.3)
* 2003-04-23 fl Fixed building for Tk 8.4.1 and later (Jack Jansen)
* 2004-06-24 fl Fixed building for Tk 8.4.6 and later.
*
* Copyright (c) 1997-2004 by Secret Labs AB
* Copyright (c) 1995-2004 by Fredrik Lundh
*
* See the README file for information on usage and redistribution.
*/
#define TKMAJORMINOR (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION)
/* This is needed for (at least) Tk 8.4.6 and later, to avoid warnings
for the Tcl_CreateCommand command. */
#define USE_COMPAT_CONST
#include "tk.h"
#include "Imaging.h"
#include <stdlib.h>
static Imaging
ImagingFind(const char* name)
{
long id;
/* FIXME: use CObject instead? */
id = atol(name);
if (!id)
return NULL;
return (Imaging) id;
}
static int
PyImagingPhotoPut(ClientData clientdata, Tcl_Interp* interp,
int argc, char **argv)
{
Imaging im;
Tk_PhotoHandle photo;
Tk_PhotoImageBlock block;
if (argc != 3) {
Tcl_AppendResult(interp, "usage: ", argv[0],
" destPhoto srcImage", (char *) NULL);
return TCL_ERROR;
}
/* get Tcl PhotoImage handle */
photo = Tk_FindPhoto(interp, argv[1]);
if (photo == NULL) {
Tcl_AppendResult(
interp, "destination photo must exist", (char *) NULL
);
return TCL_ERROR;
}
/* get PIL Image handle */
im = ImagingFind(argv[2]);
if (!im) {
Tcl_AppendResult(interp, "bad name", (char*) NULL);
return TCL_ERROR;
}
if (!im->block) {
Tcl_AppendResult(interp, "bad display memory", (char*) NULL);
return TCL_ERROR;
}
/* Active region */
#if 0
if (src_xoffset + xsize > im->xsize)
xsize = im->xsize - src_xoffset;
if (src_yoffset + ysize > im->ysize)
ysize = im->ysize - src_yoffset;
if (xsize < 0 || ysize < 0
|| src_xoffset >= im->xsize
|| src_yoffset >= im->ysize)
return TCL_OK;
#endif
/* Mode */
if (strcmp(im->mode, "1") == 0 || strcmp(im->mode, "L") == 0) {
block.pixelSize = 1;
block.offset[0] = block.offset[1] = block.offset[2] = 0;
} else if (strncmp(im->mode, "RGB", 3) == 0) {
block.pixelSize = 4;
block.offset[0] = 0;
block.offset[1] = 1;
block.offset[2] = 2;
if (strcmp(im->mode, "RGBA") == 0)
block.offset[3] = 3; /* alpha (or reserved, under 8.2) */
else
block.offset[3] = 0; /* no alpha */
} else {
Tcl_AppendResult(interp, "Bad mode", (char*) NULL);
return TCL_ERROR;
}
block.width = im->xsize;
block.height = im->ysize;
block.pitch = im->linesize;
block.pixelPtr = (unsigned char*) im->block;
#if 0
block.pixelPtr = (unsigned char*) im->block +
src_yoffset * im->linesize +
src_xoffset * im->pixelsize;
#endif
#if TKMAJORMINOR < 8004 /* Tk < 8.4.0 */
if (strcmp(im->mode, "RGBA") == 0) {
/* Copy non-transparent pixels to photo image */
int x, y;
Tk_PhotoImageBlock run;
/* Clear current contents */
Tk_PhotoBlank(photo);
/* Setup run descriptor */
run.height = 1;
run.pitch = block.pitch;
run.pixelSize = block.pixelSize;
run.offset[0] = 0;
run.offset[1] = 1;
run.offset[2] = 2;
run.offset[3] = 0; /* no alpha (or reserved, under 8.2) */
/* FIXME: handle compositing support under 8.4 and later */
/* Copy opaque runs to photo image */
for (y = 0; y < block.height; y++) {
unsigned char* p = block.pixelPtr + y*block.pitch;
unsigned char* s = p;
int w = 0;
for (x = 0; x < block.width; x++) {
if (p[3]) {
/* opaque: add pixel to current run */
if (w == 0)
s = p;
w = w + 1;
} else if (s) {
/* copy run to photo image */
if (w > 0) {
run.width = w;
run.pixelPtr = s;
Tk_PhotoPutBlock(photo, &run, x-w, y, run.width, 1);
}
w = 0;
}
p += block.pixelSize;
}
if (w > 0) {
/* copy final run, if any */
run.width = w;
run.pixelPtr = s;
Tk_PhotoPutBlock(photo, &run, x-w, y, run.width, 1);
}
}
} else
/* Copy opaque block to photo image, and leave the rest to TK */
Tk_PhotoPutBlock(photo, &block, 0, 0, block.width, block.height);
#else /* Tk >= 8.4.0 */
Tk_PhotoPutBlock(photo, &block, 0, 0, block.width, block.height,
TK_PHOTO_COMPOSITE_SET);
if (strcmp(im->mode, "RGBA") == 0)
/* Tk workaround: we need apply ToggleComplexAlphaIfNeeded */
/* (fixed in Tk 8.5a3) */
Tk_PhotoSetSize(photo, block.width, block.height);
#endif
return TCL_OK;
}
static int
PyImagingPhotoGet(ClientData clientdata, Tcl_Interp* interp,
int argc, char **argv)
{
Tk_PhotoHandle photo;
Tk_PhotoImageBlock block;
if (argc != 2) {
Tcl_AppendResult(interp, "usage: ", argv[0],
" srcPhoto", (char *) NULL);
return TCL_ERROR;
}
/* get Tcl PhotoImage handle */
photo = Tk_FindPhoto(interp, argv[1]);
if (photo == NULL) {
Tcl_AppendResult(
interp, "source photo must exist", (char *) NULL
);
return TCL_ERROR;
}
Tk_PhotoGetImage(photo, &block);
printf("pixelPtr = %p\n", block.pixelPtr);
printf("width = %d\n", block.width);
printf("height = %d\n", block.height);
printf("pitch = %d\n", block.pitch);
printf("pixelSize = %d\n", block.pixelSize);
printf("offset = %d %d %d %d\n", block.offset[0], block.offset[1],
block.offset[2], block.offset[3]);
Tcl_AppendResult(
interp, "this function is not yet supported", (char *) NULL
);
return TCL_ERROR;
}
void
TkImaging_Init(Tcl_Interp* interp)
{
Tcl_CreateCommand(interp, "PyImagingPhoto", PyImagingPhotoPut,
(ClientData) 0, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(interp, "PyImagingPhotoGet", PyImagingPhotoGet,
(ClientData) 0, (Tcl_CmdDeleteProc*) NULL);
}
|