jpayne@69
|
1 /*
|
jpayne@69
|
2 * tkImgPhoto.h --
|
jpayne@69
|
3 *
|
jpayne@69
|
4 * Declarations for images of type "photo" for Tk.
|
jpayne@69
|
5 *
|
jpayne@69
|
6 * Copyright (c) 1994 The Australian National University.
|
jpayne@69
|
7 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
|
jpayne@69
|
8 * Copyright (c) 2002-2008 Donal K. Fellows
|
jpayne@69
|
9 * Copyright (c) 2003 ActiveState Corporation.
|
jpayne@69
|
10 *
|
jpayne@69
|
11 * See the file "license.terms" for information on usage and redistribution of
|
jpayne@69
|
12 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
jpayne@69
|
13 *
|
jpayne@69
|
14 * Author: Paul Mackerras (paulus@cs.anu.edu.au),
|
jpayne@69
|
15 * Department of Computer Science,
|
jpayne@69
|
16 * Australian National University.
|
jpayne@69
|
17 */
|
jpayne@69
|
18
|
jpayne@69
|
19 #include "tkInt.h"
|
jpayne@69
|
20 #ifdef _WIN32
|
jpayne@69
|
21 #include "tkWinInt.h"
|
jpayne@69
|
22 #elif defined(__CYGWIN__)
|
jpayne@69
|
23 #include "tkUnixInt.h"
|
jpayne@69
|
24 #endif
|
jpayne@69
|
25
|
jpayne@69
|
26 /*
|
jpayne@69
|
27 * Forward declarations of the structures we define.
|
jpayne@69
|
28 */
|
jpayne@69
|
29
|
jpayne@69
|
30 #define PhotoModel PhotoMaster
|
jpayne@69
|
31 typedef struct ColorTableId ColorTableId;
|
jpayne@69
|
32 typedef struct ColorTable ColorTable;
|
jpayne@69
|
33 typedef struct PhotoInstance PhotoInstance;
|
jpayne@69
|
34 typedef struct PhotoMaster PhotoMaster;
|
jpayne@69
|
35
|
jpayne@69
|
36 /*
|
jpayne@69
|
37 * A signed 8-bit integral type. If chars are unsigned and the compiler isn't
|
jpayne@69
|
38 * an ANSI one, then we have to use short instead (which wastes space) to get
|
jpayne@69
|
39 * signed behavior.
|
jpayne@69
|
40 */
|
jpayne@69
|
41
|
jpayne@69
|
42 #if defined(__STDC__) || defined(_AIX)
|
jpayne@69
|
43 typedef signed char schar;
|
jpayne@69
|
44 #else
|
jpayne@69
|
45 # ifndef __CHAR_UNSIGNED__
|
jpayne@69
|
46 typedef char schar;
|
jpayne@69
|
47 # else
|
jpayne@69
|
48 typedef short schar;
|
jpayne@69
|
49 # endif
|
jpayne@69
|
50 #endif
|
jpayne@69
|
51
|
jpayne@69
|
52 /*
|
jpayne@69
|
53 * An unsigned 32-bit integral type, used for pixel values. We use int rather
|
jpayne@69
|
54 * than long here to accommodate those systems where longs are 64 bits.
|
jpayne@69
|
55 */
|
jpayne@69
|
56
|
jpayne@69
|
57 typedef unsigned int pixel;
|
jpayne@69
|
58
|
jpayne@69
|
59 /*
|
jpayne@69
|
60 * The maximum number of pixels to transmit to the server in a single
|
jpayne@69
|
61 * XPutImage call.
|
jpayne@69
|
62 */
|
jpayne@69
|
63
|
jpayne@69
|
64 #define MAX_PIXELS 65536
|
jpayne@69
|
65
|
jpayne@69
|
66 /*
|
jpayne@69
|
67 * The set of colors required to display a photo image in a window depends on:
|
jpayne@69
|
68 * - the visual used by the window
|
jpayne@69
|
69 * - the palette, which specifies how many levels of each primary color to
|
jpayne@69
|
70 * use, and
|
jpayne@69
|
71 * - the gamma value for the image.
|
jpayne@69
|
72 *
|
jpayne@69
|
73 * Pixel values allocated for specific colors are valid only for the colormap
|
jpayne@69
|
74 * in which they were allocated. Sets of pixel values allocated for displaying
|
jpayne@69
|
75 * photos are re-used in other windows if possible, that is, if the display,
|
jpayne@69
|
76 * colormap, palette and gamma values match. A hash table is used to locate
|
jpayne@69
|
77 * these sets of pixel values, using the following data structure as key:
|
jpayne@69
|
78 */
|
jpayne@69
|
79
|
jpayne@69
|
80 struct ColorTableId {
|
jpayne@69
|
81 Display *display; /* Qualifies the colormap resource ID. */
|
jpayne@69
|
82 Colormap colormap; /* Colormap that the windows are using. */
|
jpayne@69
|
83 double gamma; /* Gamma exponent value for images. */
|
jpayne@69
|
84 Tk_Uid palette; /* Specifies how many shades of each primary
|
jpayne@69
|
85 * we want to allocate. */
|
jpayne@69
|
86 };
|
jpayne@69
|
87
|
jpayne@69
|
88 /*
|
jpayne@69
|
89 * For a particular (display, colormap, palette, gamma) combination, a data
|
jpayne@69
|
90 * structure of the following type is used to store the allocated pixel values
|
jpayne@69
|
91 * and other information:
|
jpayne@69
|
92 */
|
jpayne@69
|
93
|
jpayne@69
|
94 struct ColorTable {
|
jpayne@69
|
95 ColorTableId id; /* Information used in selecting this color
|
jpayne@69
|
96 * table. */
|
jpayne@69
|
97 int flags; /* See below. */
|
jpayne@69
|
98 int refCount; /* Number of instances using this map. */
|
jpayne@69
|
99 int liveRefCount; /* Number of instances which are actually in
|
jpayne@69
|
100 * use, using this map. */
|
jpayne@69
|
101 int numColors; /* Number of colors allocated for this map. */
|
jpayne@69
|
102
|
jpayne@69
|
103 XVisualInfo visualInfo; /* Information about the visual for windows
|
jpayne@69
|
104 * using this color table. */
|
jpayne@69
|
105
|
jpayne@69
|
106 pixel redValues[256]; /* Maps 8-bit values of red intensity to a
|
jpayne@69
|
107 * pixel value or index in pixelMap. */
|
jpayne@69
|
108 pixel greenValues[256]; /* Ditto for green intensity. */
|
jpayne@69
|
109 pixel blueValues[256]; /* Ditto for blue intensity. */
|
jpayne@69
|
110 unsigned long *pixelMap; /* Actual pixel values allocated. */
|
jpayne@69
|
111
|
jpayne@69
|
112 unsigned char colorQuant[3][256];
|
jpayne@69
|
113 /* Maps 8-bit intensities to quantized
|
jpayne@69
|
114 * intensities. The first index is 0 for red,
|
jpayne@69
|
115 * 1 for green, 2 for blue. */
|
jpayne@69
|
116 };
|
jpayne@69
|
117
|
jpayne@69
|
118 /*
|
jpayne@69
|
119 * Bit definitions for the flags field of a ColorTable.
|
jpayne@69
|
120 * BLACK_AND_WHITE: 1 means only black and white colors are
|
jpayne@69
|
121 * available.
|
jpayne@69
|
122 * COLOR_WINDOW: 1 means a full 3-D color cube has been
|
jpayne@69
|
123 * allocated.
|
jpayne@69
|
124 * DISPOSE_PENDING: 1 means a call to DisposeColorTable has been
|
jpayne@69
|
125 * scheduled as an idle handler, but it hasn't
|
jpayne@69
|
126 * been invoked yet.
|
jpayne@69
|
127 * MAP_COLORS: 1 means pixel values should be mapped through
|
jpayne@69
|
128 * pixelMap.
|
jpayne@69
|
129 */
|
jpayne@69
|
130
|
jpayne@69
|
131 #ifdef COLOR_WINDOW
|
jpayne@69
|
132 #undef COLOR_WINDOW
|
jpayne@69
|
133 #endif
|
jpayne@69
|
134
|
jpayne@69
|
135 #define BLACK_AND_WHITE 1
|
jpayne@69
|
136 #define COLOR_WINDOW 2
|
jpayne@69
|
137 #define DISPOSE_PENDING 4
|
jpayne@69
|
138 #define MAP_COLORS 8
|
jpayne@69
|
139
|
jpayne@69
|
140 /*
|
jpayne@69
|
141 * Definition of the data associated with each photo image model.
|
jpayne@69
|
142 */
|
jpayne@69
|
143
|
jpayne@69
|
144 struct PhotoMaster {
|
jpayne@69
|
145 Tk_ImageMaster tkMaster; /* Tk's token for image model. NULL means the
|
jpayne@69
|
146 * image is being deleted. */
|
jpayne@69
|
147 Tcl_Interp *interp; /* Interpreter associated with the application
|
jpayne@69
|
148 * using this image. */
|
jpayne@69
|
149 Tcl_Command imageCmd; /* Token for image command (used to delete it
|
jpayne@69
|
150 * when the image goes away). NULL means the
|
jpayne@69
|
151 * image command has already been deleted. */
|
jpayne@69
|
152 int flags; /* Sundry flags, defined below. */
|
jpayne@69
|
153 int width, height; /* Dimensions of image. */
|
jpayne@69
|
154 int userWidth, userHeight; /* User-declared image dimensions. */
|
jpayne@69
|
155 Tk_Uid palette; /* User-specified default palette for
|
jpayne@69
|
156 * instances of this image. */
|
jpayne@69
|
157 double gamma; /* Display gamma value to correct for. */
|
jpayne@69
|
158 char *fileString; /* Name of file to read into image. */
|
jpayne@69
|
159 Tcl_Obj *dataString; /* Object to use as contents of image. */
|
jpayne@69
|
160 Tcl_Obj *format; /* User-specified format of data in image file
|
jpayne@69
|
161 * or string value. */
|
jpayne@69
|
162 unsigned char *pix32; /* Local storage for 32-bit image. */
|
jpayne@69
|
163 int ditherX, ditherY; /* Location of first incorrectly dithered
|
jpayne@69
|
164 * pixel in image. */
|
jpayne@69
|
165 TkRegion validRegion; /* Tk region indicating which parts of the
|
jpayne@69
|
166 * image have valid image data. */
|
jpayne@69
|
167 PhotoInstance *instancePtr; /* First in the list of instances associated
|
jpayne@69
|
168 * with this model. */
|
jpayne@69
|
169 };
|
jpayne@69
|
170
|
jpayne@69
|
171 /*
|
jpayne@69
|
172 * Bit definitions for the flags field of a PhotoMaster.
|
jpayne@69
|
173 * COLOR_IMAGE: 1 means that the image has different color
|
jpayne@69
|
174 * components.
|
jpayne@69
|
175 * IMAGE_CHANGED: 1 means that the instances of this image need
|
jpayne@69
|
176 * to be redithered.
|
jpayne@69
|
177 * COMPLEX_ALPHA: 1 means that the instances of this image have
|
jpayne@69
|
178 * alpha values that aren't 0 or 255, and so need
|
jpayne@69
|
179 * the copy-merge-replace renderer .
|
jpayne@69
|
180 */
|
jpayne@69
|
181
|
jpayne@69
|
182 #define COLOR_IMAGE 1
|
jpayne@69
|
183 #define IMAGE_CHANGED 2
|
jpayne@69
|
184 #define COMPLEX_ALPHA 4
|
jpayne@69
|
185
|
jpayne@69
|
186 /*
|
jpayne@69
|
187 * Flag to OR with the compositing rule to indicate that the source, despite
|
jpayne@69
|
188 * having an alpha channel, has simple alpha.
|
jpayne@69
|
189 */
|
jpayne@69
|
190
|
jpayne@69
|
191 #define SOURCE_IS_SIMPLE_ALPHA_PHOTO 0x10000000
|
jpayne@69
|
192
|
jpayne@69
|
193 /*
|
jpayne@69
|
194 * The following data structure represents all of the instances of a photo
|
jpayne@69
|
195 * image in windows on a given screen that are using the same colormap.
|
jpayne@69
|
196 */
|
jpayne@69
|
197
|
jpayne@69
|
198 struct PhotoInstance {
|
jpayne@69
|
199 PhotoMaster *masterPtr; /* Pointer to model for image. */
|
jpayne@69
|
200 Display *display; /* Display for windows using this instance. */
|
jpayne@69
|
201 Colormap colormap; /* The image may only be used in windows with
|
jpayne@69
|
202 * this particular colormap. */
|
jpayne@69
|
203 PhotoInstance *nextPtr; /* Pointer to the next instance in the list of
|
jpayne@69
|
204 * instances associated with this model. */
|
jpayne@69
|
205 int refCount; /* Number of instances using this structure. */
|
jpayne@69
|
206 Tk_Uid palette; /* Palette for these particular instances. */
|
jpayne@69
|
207 double gamma; /* Gamma value for these instances. */
|
jpayne@69
|
208 Tk_Uid defaultPalette; /* Default palette to use if a palette is not
|
jpayne@69
|
209 * specified for the model. */
|
jpayne@69
|
210 ColorTable *colorTablePtr; /* Pointer to information about colors
|
jpayne@69
|
211 * allocated for image display in windows like
|
jpayne@69
|
212 * this one. */
|
jpayne@69
|
213 Pixmap pixels; /* X pixmap containing dithered image. */
|
jpayne@69
|
214 int width, height; /* Dimensions of the pixmap. */
|
jpayne@69
|
215 schar *error; /* Error image, used in dithering. */
|
jpayne@69
|
216 XImage *imagePtr; /* Image structure for converted pixels. */
|
jpayne@69
|
217 XVisualInfo visualInfo; /* Information about the visual that these
|
jpayne@69
|
218 * windows are using. */
|
jpayne@69
|
219 GC gc; /* Graphics context for writing images to the
|
jpayne@69
|
220 * pixmap. */
|
jpayne@69
|
221 };
|
jpayne@69
|
222
|
jpayne@69
|
223 /*
|
jpayne@69
|
224 * Implementation of the Porter-Duff Source-Over compositing rule.
|
jpayne@69
|
225 */
|
jpayne@69
|
226
|
jpayne@69
|
227 #define PD_SRC_OVER(srcColor, srcAlpha, dstColor, dstAlpha) \
|
jpayne@69
|
228 (srcColor*srcAlpha/255) + dstAlpha*(255-srcAlpha)/255*dstColor/255
|
jpayne@69
|
229 #define PD_SRC_OVER_ALPHA(srcAlpha, dstAlpha) \
|
jpayne@69
|
230 (srcAlpha + (255-srcAlpha)*dstAlpha/255)
|
jpayne@69
|
231
|
jpayne@69
|
232 #undef MIN
|
jpayne@69
|
233 #define MIN(a, b) ((a) < (b)? (a): (b))
|
jpayne@69
|
234 #undef MAX
|
jpayne@69
|
235 #define MAX(a, b) ((a) > (b)? (a): (b))
|
jpayne@69
|
236
|
jpayne@69
|
237 /*
|
jpayne@69
|
238 * Declarations of functions shared between the different parts of the
|
jpayne@69
|
239 * photo image implementation.
|
jpayne@69
|
240 */
|
jpayne@69
|
241
|
jpayne@69
|
242 MODULE_SCOPE void TkImgPhotoConfigureInstance(
|
jpayne@69
|
243 PhotoInstance *instancePtr);
|
jpayne@69
|
244 MODULE_SCOPE void TkImgDisposeInstance(ClientData clientData);
|
jpayne@69
|
245 MODULE_SCOPE void TkImgPhotoInstanceSetSize(PhotoInstance *instancePtr);
|
jpayne@69
|
246 MODULE_SCOPE ClientData TkImgPhotoGet(Tk_Window tkwin, ClientData clientData);
|
jpayne@69
|
247 MODULE_SCOPE void TkImgDitherInstance(PhotoInstance *instancePtr, int x,
|
jpayne@69
|
248 int y, int width, int height);
|
jpayne@69
|
249 MODULE_SCOPE void TkImgPhotoDisplay(ClientData clientData,
|
jpayne@69
|
250 Display *display, Drawable drawable,
|
jpayne@69
|
251 int imageX, int imageY, int width, int height,
|
jpayne@69
|
252 int drawableX, int drawableY);
|
jpayne@69
|
253 MODULE_SCOPE void TkImgPhotoFree(ClientData clientData,
|
jpayne@69
|
254 Display *display);
|
jpayne@69
|
255 MODULE_SCOPE void TkImgResetDither(PhotoInstance *instancePtr);
|
jpayne@69
|
256
|
jpayne@69
|
257 /*
|
jpayne@69
|
258 * Local Variables:
|
jpayne@69
|
259 * mode: c
|
jpayne@69
|
260 * c-basic-offset: 4
|
jpayne@69
|
261 * fill-column: 78
|
jpayne@69
|
262 * End:
|
jpayne@69
|
263 */
|