tkImgPhoto.h 8.8 KB

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