miindex.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. *
  3. * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and its
  6. * documentation for any purpose is hereby granted without fee, provided that
  7. * the above copyright notice appear in all copies and that both that
  8. * copyright notice and this permission notice appear in supporting
  9. * documentation, and that the name of Keith Packard not be used in
  10. * advertising or publicity pertaining to distribution of the software without
  11. * specific, written prior permission. Keith Packard makes no
  12. * representations about the suitability of this software for any purpose. It
  13. * is provided "as is" without express or implied warranty.
  14. *
  15. * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  20. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. * PERFORMANCE OF THIS SOFTWARE.
  22. */
  23. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #ifndef _MIINDEX_H_
  27. #define _MIINDEX_H_
  28. #include "scrnintstr.h"
  29. #include "gcstruct.h"
  30. #include "pixmapstr.h"
  31. #include "windowstr.h"
  32. #include "mi.h"
  33. #include "picturestr.h"
  34. #include "mipict.h"
  35. #include "colormapst.h"
  36. #define NUM_CUBE_LEVELS 4
  37. #define NUM_GRAY_LEVELS 13
  38. static Bool
  39. miBuildRenderColormap(ColormapPtr pColormap, Pixel * pixels, int *nump)
  40. {
  41. int r, g, b;
  42. unsigned short red, green, blue;
  43. Pixel pixel;
  44. Bool used[MI_MAX_INDEXED];
  45. int needed;
  46. int policy;
  47. int cube, gray;
  48. int i, n;
  49. if (pColormap->mid != pColormap->pScreen->defColormap) {
  50. policy = PictureCmapPolicyAll;
  51. }
  52. else {
  53. int avail = pColormap->pVisual->ColormapEntries;
  54. policy = PictureCmapPolicy;
  55. if (policy == PictureCmapPolicyDefault) {
  56. if (avail >= 256 &&
  57. (pColormap->pVisual->class | DynamicClass) == PseudoColor)
  58. policy = PictureCmapPolicyColor;
  59. else if (avail >= 64)
  60. policy = PictureCmapPolicyGray;
  61. else
  62. policy = PictureCmapPolicyMono;
  63. }
  64. }
  65. /*
  66. * Make sure enough cells are free for the chosen policy
  67. */
  68. for (;;) {
  69. switch (policy) {
  70. case PictureCmapPolicyAll:
  71. needed = 0;
  72. break;
  73. case PictureCmapPolicyColor:
  74. needed = 71;
  75. break;
  76. case PictureCmapPolicyGray:
  77. needed = 11;
  78. break;
  79. case PictureCmapPolicyMono:
  80. default:
  81. needed = 0;
  82. break;
  83. }
  84. if (needed <= pColormap->freeRed)
  85. break;
  86. policy--;
  87. }
  88. /*
  89. * Compute size of cube and gray ramps
  90. */
  91. cube = gray = 0;
  92. switch (policy) {
  93. case PictureCmapPolicyAll:
  94. /*
  95. * Allocate as big a cube as possible
  96. */
  97. if ((pColormap->pVisual->class | DynamicClass) == PseudoColor) {
  98. for (cube = 1;
  99. cube * cube * cube < pColormap->pVisual->ColormapEntries;
  100. cube++);
  101. cube--;
  102. if (cube == 1)
  103. cube = 0;
  104. }
  105. else
  106. cube = 0;
  107. /*
  108. * Figure out how many gray levels to use so that they
  109. * line up neatly with the cube
  110. */
  111. if (cube) {
  112. needed = pColormap->pVisual->ColormapEntries - (cube * cube * cube);
  113. /* levels to fill in with */
  114. gray = needed / (cube - 1);
  115. /* total levels */
  116. gray = (gray + 1) * (cube - 1) + 1;
  117. }
  118. else
  119. gray = pColormap->pVisual->ColormapEntries;
  120. break;
  121. case PictureCmapPolicyColor:
  122. cube = NUM_CUBE_LEVELS;
  123. /* fall through ... */
  124. case PictureCmapPolicyGray:
  125. gray = NUM_GRAY_LEVELS;
  126. break;
  127. case PictureCmapPolicyMono:
  128. default:
  129. gray = 2;
  130. break;
  131. }
  132. memset(used, '\0', pColormap->pVisual->ColormapEntries * sizeof(Bool));
  133. for (r = 0; r < cube; r++)
  134. for (g = 0; g < cube; g++)
  135. for (b = 0; b < cube; b++) {
  136. pixel = 0;
  137. red = (r * 65535 + (cube - 1) / 2) / (cube - 1);
  138. green = (g * 65535 + (cube - 1) / 2) / (cube - 1);
  139. blue = (b * 65535 + (cube - 1) / 2) / (cube - 1);
  140. if (AllocColor(pColormap, &red, &green,
  141. &blue, &pixel, 0) != Success)
  142. return FALSE;
  143. used[pixel] = TRUE;
  144. }
  145. for (g = 0; g < gray; g++) {
  146. pixel = 0;
  147. red = green = blue = (g * 65535 + (gray - 1) / 2) / (gray - 1);
  148. if (AllocColor(pColormap, &red, &green, &blue, &pixel, 0) != Success)
  149. return FALSE;
  150. used[pixel] = TRUE;
  151. }
  152. n = 0;
  153. for (i = 0; i < pColormap->pVisual->ColormapEntries; i++)
  154. if (used[i])
  155. pixels[n++] = i;
  156. *nump = n;
  157. return TRUE;
  158. }
  159. /* 0 <= red, green, blue < 32 */
  160. static Pixel
  161. FindBestColor(miIndexedPtr pIndexed, Pixel * pixels, int num,
  162. int red, int green, int blue)
  163. {
  164. Pixel best = pixels[0];
  165. int bestDist = 1 << 30;
  166. int dist;
  167. int dr, dg, db;
  168. while (num--) {
  169. Pixel pixel = *pixels++;
  170. CARD32 v = pIndexed->rgba[pixel];
  171. dr = ((v >> 19) & 0x1f);
  172. dg = ((v >> 11) & 0x1f);
  173. db = ((v >> 3) & 0x1f);
  174. dr = dr - red;
  175. dg = dg - green;
  176. db = db - blue;
  177. dist = dr * dr + dg * dg + db * db;
  178. if (dist < bestDist) {
  179. bestDist = dist;
  180. best = pixel;
  181. }
  182. }
  183. return best;
  184. }
  185. /* 0 <= gray < 32768 */
  186. static Pixel
  187. FindBestGray(miIndexedPtr pIndexed, Pixel * pixels, int num, int gray)
  188. {
  189. Pixel best = pixels[0];
  190. int bestDist = 1 << 30;
  191. int dist;
  192. int dr;
  193. int r;
  194. while (num--) {
  195. Pixel pixel = *pixels++;
  196. CARD32 v = pIndexed->rgba[pixel];
  197. r = v & 0xff;
  198. r = r | (r << 8);
  199. dr = gray - (r >> 1);
  200. dist = dr * dr;
  201. if (dist < bestDist) {
  202. bestDist = dist;
  203. best = pixel;
  204. }
  205. }
  206. return best;
  207. }
  208. Bool
  209. miInitIndexed(ScreenPtr pScreen, PictFormatPtr pFormat)
  210. {
  211. ColormapPtr pColormap = pFormat->index.pColormap;
  212. VisualPtr pVisual = pColormap->pVisual;
  213. miIndexedPtr pIndexed;
  214. Pixel pixels[MI_MAX_INDEXED];
  215. xrgb rgb[MI_MAX_INDEXED];
  216. int num = 0;
  217. int i;
  218. Pixel p, r, g, b;
  219. if (pVisual->ColormapEntries > MI_MAX_INDEXED)
  220. return FALSE;
  221. if (pVisual->class & DynamicClass) {
  222. if (!miBuildRenderColormap(pColormap, pixels, &num))
  223. return FALSE;
  224. }
  225. else {
  226. num = pVisual->ColormapEntries;
  227. for (p = 0; p < num; p++)
  228. pixels[p] = p;
  229. }
  230. pIndexed = malloc(sizeof(miIndexedRec));
  231. if (!pIndexed)
  232. return FALSE;
  233. pFormat->index.nvalues = num;
  234. pFormat->index.pValues = malloc(num * sizeof(xIndexValue));
  235. if (!pFormat->index.pValues || !num) {
  236. free(pIndexed);
  237. return FALSE;
  238. }
  239. /*
  240. * Build mapping from pixel value to ARGB
  241. */
  242. QueryColors(pColormap, num, pixels, rgb);
  243. for (i = 0; i < num; i++) {
  244. p = pixels[i];
  245. pFormat->index.pValues[i].pixel = p;
  246. pFormat->index.pValues[i].red = rgb[i].red;
  247. pFormat->index.pValues[i].green = rgb[i].green;
  248. pFormat->index.pValues[i].blue = rgb[i].blue;
  249. pFormat->index.pValues[i].alpha = 0xffff;
  250. pIndexed->rgba[p] = (0xff000000 |
  251. ((rgb[i].red & 0xff00) << 8) |
  252. ((rgb[i].green & 0xff00)) |
  253. ((rgb[i].blue & 0xff00) >> 8));
  254. }
  255. /*
  256. * Build mapping from RGB to pixel value. This could probably be
  257. * done a bit quicker...
  258. */
  259. switch (pVisual->class | DynamicClass) {
  260. case GrayScale:
  261. pIndexed->color = FALSE;
  262. for (r = 0; r < 32768; r++)
  263. pIndexed->ent[r] = FindBestGray(pIndexed, pixels, num, r);
  264. break;
  265. case PseudoColor:
  266. pIndexed->color = TRUE;
  267. p = 0;
  268. for (r = 0; r < 32; r++)
  269. for (g = 0; g < 32; g++)
  270. for (b = 0; b < 32; b++) {
  271. pIndexed->ent[p] = FindBestColor(pIndexed, pixels, num,
  272. r, g, b);
  273. p++;
  274. }
  275. break;
  276. }
  277. pFormat->index.devPrivate = pIndexed;
  278. return TRUE;
  279. }
  280. void
  281. miCloseIndexed(ScreenPtr pScreen, PictFormatPtr pFormat)
  282. {
  283. if (pFormat->index.devPrivate) {
  284. free(pFormat->index.devPrivate);
  285. pFormat->index.devPrivate = 0;
  286. }
  287. if (pFormat->index.pValues) {
  288. free(pFormat->index.pValues);
  289. pFormat->index.pValues = 0;
  290. }
  291. }
  292. void
  293. miUpdateIndexed(ScreenPtr pScreen,
  294. PictFormatPtr pFormat, int ndef, xColorItem * pdef)
  295. {
  296. miIndexedPtr pIndexed = pFormat->index.devPrivate;
  297. if (pIndexed) {
  298. while (ndef--) {
  299. pIndexed->rgba[pdef->pixel] = (0xff000000 |
  300. ((pdef->red & 0xff00) << 8) |
  301. ((pdef->green & 0xff00)) |
  302. ((pdef->blue & 0xff00) >> 8));
  303. pdef++;
  304. }
  305. }
  306. }
  307. #endif /* _MIINDEX_H_ */