| 1 |
/* |
| 2 |
* tkUnixPmap.c -- |
| 3 |
* |
| 4 |
* Implement the Unix specific function calls for the pixmap |
| 5 |
* image type. |
| 6 |
* |
| 7 |
* Copyright (c) 1996, Expert Interface Technologies |
| 8 |
* |
| 9 |
* See the file "license.terms" for information on usage and redistribution |
| 10 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <tk.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <imgPmap.h> |
| 17 |
#include <X11/Xlib.h> |
| 18 |
#include <X11/Xutil.h> |
| 19 |
|
| 20 |
typedef struct PixmapData { |
| 21 |
Pixmap mask; /* Mask: only display pixmap pixels where |
| 22 |
* there are 1's here. */ |
| 23 |
GC gc; /* Graphics context for displaying pixmap. |
| 24 |
* None means there was an error while |
| 25 |
* setting up the instance, so it cannot |
| 26 |
* be displayed. */ |
| 27 |
} PixmapData; |
| 28 |
|
| 29 |
|
| 30 |
/*---------------------------------------------------------------------- |
| 31 |
* ImgInitPixmapInstance -- |
| 32 |
* |
| 33 |
* Initializes the platform-specific data of a pixmap instance |
| 34 |
* |
| 35 |
*---------------------------------------------------------------------- |
| 36 |
*/ |
| 37 |
|
| 38 |
void |
| 39 |
ImgInitPixmapInstance(masterPtr, instancePtr) |
| 40 |
PixmapMaster *masterPtr; /* Pointer to master for image. */ |
| 41 |
PixmapInstance *instancePtr;/* The pixmap instance. */ |
| 42 |
{ |
| 43 |
PixmapData * dataPtr; |
| 44 |
|
| 45 |
dataPtr = (PixmapData *)ckalloc(sizeof(PixmapData)); |
| 46 |
dataPtr->mask = None; |
| 47 |
dataPtr->gc = None; |
| 48 |
|
| 49 |
instancePtr->clientData = (ClientData)dataPtr; |
| 50 |
} |
| 51 |
|
| 52 |
/*---------------------------------------------------------------------- |
| 53 |
* ImgXpmAllocTmpBuffer -- |
| 54 |
* |
| 55 |
* Allocate a temporary space to draw the image. |
| 56 |
* |
| 57 |
*---------------------------------------------------------------------- |
| 58 |
*/ |
| 59 |
|
| 60 |
void |
| 61 |
ImgXpmAllocTmpBuffer(masterPtr, instancePtr, imagePtr, maskPtr) |
| 62 |
PixmapMaster * masterPtr; |
| 63 |
PixmapInstance * instancePtr; |
| 64 |
XImage ** imagePtr; |
| 65 |
XImage ** maskPtr; |
| 66 |
{ |
| 67 |
int pad; |
| 68 |
XImage * image = NULL, * mask = NULL; |
| 69 |
Display *display = Tk_Display(instancePtr->tkwin); |
| 70 |
int depth; |
| 71 |
|
| 72 |
depth = Tk_Depth(instancePtr->tkwin); |
| 73 |
|
| 74 |
if (depth > 16) { |
| 75 |
pad = 32; |
| 76 |
} else if (depth > 8) { |
| 77 |
pad = 16; |
| 78 |
} else { |
| 79 |
pad = 8; |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* Create the XImage structures to store the temporary image |
| 84 |
*/ |
| 85 |
image = XCreateImage(display, |
| 86 |
Tk_Visual(instancePtr->tkwin), |
| 87 |
depth, ZPixmap, 0, 0, |
| 88 |
masterPtr->size[0], masterPtr->size[1], pad, 0); |
| 89 |
image->data = |
| 90 |
(char *)ckalloc(image->bytes_per_line * masterPtr->size[1]); |
| 91 |
|
| 92 |
mask = XCreateImage(display, |
| 93 |
Tk_Visual(instancePtr->tkwin), |
| 94 |
1, XYPixmap, 0, 0, |
| 95 |
masterPtr->size[0], masterPtr->size[1], pad, 0); |
| 96 |
|
| 97 |
mask->data = |
| 98 |
(char *)ckalloc(mask->bytes_per_line * masterPtr->size[1]); |
| 99 |
|
| 100 |
*imagePtr = image; |
| 101 |
*maskPtr = mask; |
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
ImgXpmFreeTmpBuffer(masterPtr, instancePtr, image, mask) |
| 106 |
PixmapMaster * masterPtr; |
| 107 |
PixmapInstance * instancePtr; |
| 108 |
XImage * image; |
| 109 |
XImage * mask; |
| 110 |
{ |
| 111 |
if (image) { |
| 112 |
ckfree((char*)image->data); |
| 113 |
image->data = NULL; |
| 114 |
XDestroyImage(image); |
| 115 |
} |
| 116 |
if (mask) { |
| 117 |
ckfree((char*)mask->data); |
| 118 |
mask->data = NULL; |
| 119 |
XDestroyImage(mask); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/*---------------------------------------------------------------------- |
| 124 |
* ImgXpmSetPixel -- |
| 125 |
* |
| 126 |
* Sets the pixel at the given (x,y) coordinate to be the given |
| 127 |
* color. |
| 128 |
*---------------------------------------------------------------------- |
| 129 |
*/ |
| 130 |
void |
| 131 |
ImgXpmSetPixel(instancePtr, image, mask, x, y, colorPtr, isTranspPtr) |
| 132 |
PixmapInstance * instancePtr; |
| 133 |
XImage * image; |
| 134 |
XImage * mask; |
| 135 |
int x; |
| 136 |
int y; |
| 137 |
XColor * colorPtr; |
| 138 |
int * isTranspPtr; |
| 139 |
{ |
| 140 |
if (colorPtr != NULL) { |
| 141 |
XPutPixel(image, x, y, colorPtr->pixel); |
| 142 |
XPutPixel(mask, x, y, 1); |
| 143 |
} else { |
| 144 |
XPutPixel(mask, x, y, 0); |
| 145 |
*isTranspPtr = 1; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/*---------------------------------------------------------------------- |
| 150 |
* ImgXpmRealizePixmap -- |
| 151 |
* |
| 152 |
* On Unix: Create the pixmap from the buffer. |
| 153 |
* On Windows: Free the mask if there are no transparent pixels. |
| 154 |
*---------------------------------------------------------------------- |
| 155 |
*/ |
| 156 |
|
| 157 |
void |
| 158 |
ImgXpmRealizePixmap(masterPtr, instancePtr, image, mask, isTransp) |
| 159 |
PixmapMaster * masterPtr; |
| 160 |
PixmapInstance * instancePtr; |
| 161 |
XImage * image; |
| 162 |
XImage * mask; |
| 163 |
int isTransp; |
| 164 |
{ |
| 165 |
Display *display = Tk_Display(instancePtr->tkwin); |
| 166 |
int depth = Tk_Depth(instancePtr->tkwin); |
| 167 |
PixmapData *dataPtr = (PixmapData*)instancePtr->clientData; |
| 168 |
unsigned int gcMask; |
| 169 |
XGCValues gcValues; |
| 170 |
GC gc; |
| 171 |
|
| 172 |
instancePtr->pixmap = Tk_GetPixmap(display, |
| 173 |
Tk_WindowId(instancePtr->tkwin), |
| 174 |
masterPtr->size[0], masterPtr->size[1], depth); |
| 175 |
|
| 176 |
gc = Tk_GetGC(instancePtr->tkwin, 0, NULL); |
| 177 |
|
| 178 |
XPutImage(display, instancePtr->pixmap, |
| 179 |
gc, image, 0, 0, 0, 0, masterPtr->size[0], masterPtr->size[1]); |
| 180 |
|
| 181 |
Tk_FreeGC(display, gc); |
| 182 |
|
| 183 |
if (isTransp) { |
| 184 |
/* |
| 185 |
* There are transparent pixels. We need a mask. |
| 186 |
*/ |
| 187 |
dataPtr->mask = Tk_GetPixmap(display, |
| 188 |
Tk_WindowId(instancePtr->tkwin), |
| 189 |
masterPtr->size[0], masterPtr->size[1], 1); |
| 190 |
gc = XCreateGC(display, dataPtr->mask, 0, NULL); |
| 191 |
XPutImage(display, dataPtr->mask, |
| 192 |
gc, mask, 0, 0, 0, 0, masterPtr->size[0], masterPtr->size[1]); |
| 193 |
XFreeGC(display, gc); |
| 194 |
} else { |
| 195 |
dataPtr->mask = None; |
| 196 |
} |
| 197 |
|
| 198 |
/* |
| 199 |
* Allocate a GC for drawing this instance (mask is not used if there |
| 200 |
* is no transparent pixels inside the image). |
| 201 |
*/ |
| 202 |
if (dataPtr->mask != None) { |
| 203 |
gcMask = GCGraphicsExposures|GCClipMask; |
| 204 |
} else { |
| 205 |
gcMask = GCGraphicsExposures; |
| 206 |
} |
| 207 |
gcValues.graphics_exposures = False; |
| 208 |
gcValues.clip_mask = dataPtr->mask; |
| 209 |
|
| 210 |
gc = Tk_GetGC(instancePtr->tkwin, gcMask, &gcValues); |
| 211 |
dataPtr->gc = gc; |
| 212 |
} |
| 213 |
|
| 214 |
void |
| 215 |
ImgXpmFreeInstanceData(instancePtr, delete) |
| 216 |
PixmapInstance *instancePtr; /* Pixmap instance. */ |
| 217 |
int delete; /* Should the instance data structure |
| 218 |
* be deleted as well? */ |
| 219 |
{ |
| 220 |
PixmapData *dataPtr = (PixmapData*)instancePtr->clientData; |
| 221 |
|
| 222 |
if (dataPtr->mask != None) { |
| 223 |
Tk_FreePixmap(Tk_Display(instancePtr->tkwin), dataPtr->mask); |
| 224 |
dataPtr->mask = None; |
| 225 |
} |
| 226 |
if (dataPtr->gc != None) { |
| 227 |
Tk_FreeGC(Tk_Display(instancePtr->tkwin), dataPtr->gc); |
| 228 |
dataPtr->gc = None; |
| 229 |
} |
| 230 |
if (delete) { |
| 231 |
ckfree((char*)dataPtr); |
| 232 |
instancePtr->clientData = NULL; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
void |
| 237 |
ImgpXpmDisplay(clientData, display, drawable, imageX, imageY, width, |
| 238 |
height, drawableX, drawableY) |
| 239 |
ClientData clientData; /* Pointer to PixmapInstance structure for |
| 240 |
* for instance to be displayed. */ |
| 241 |
Display *display; /* Display on which to draw image. */ |
| 242 |
Drawable drawable; /* Pixmap or window in which to draw image. */ |
| 243 |
int imageX, imageY; /* Upper-left corner of region within image |
| 244 |
* to draw. */ |
| 245 |
int width, height; /* Dimensions of region within image to draw.*/ |
| 246 |
int drawableX, drawableY; /* Coordinates within drawable that |
| 247 |
* correspond to imageX and imageY. */ |
| 248 |
{ |
| 249 |
PixmapInstance *instancePtr = (PixmapInstance *) clientData; |
| 250 |
PixmapData *dataPtr = (PixmapData*)instancePtr->clientData; |
| 251 |
|
| 252 |
/* |
| 253 |
* If there's no graphics context, it means that an error occurred |
| 254 |
* while creating the image instance so it can't be displayed. |
| 255 |
*/ |
| 256 |
if (dataPtr->gc == None) { |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
* We always use clipping: modify the clip origin within |
| 262 |
* the graphics context to line up with the image's origin. |
| 263 |
* Then draw the image and reset the clip origin. |
| 264 |
*/ |
| 265 |
XSetClipOrigin(display, dataPtr->gc, drawableX - imageX, |
| 266 |
drawableY - imageY); |
| 267 |
XCopyArea(display, instancePtr->pixmap, drawable, dataPtr->gc, |
| 268 |
imageX, imageY, (unsigned) width, (unsigned) height, |
| 269 |
drawableX, drawableY); |
| 270 |
XSetClipOrigin(display, dataPtr->gc, 0, 0); |
| 271 |
} |