1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | #ifdef HAVE_DIX_CONFIG_H1 |
25 | #include <dix-config.h> |
26 | #endif |
27 | |
28 | #ifndef _MIINDEX_H_ |
29 | #define _MIINDEX_H_ |
30 | |
31 | #include "scrnintstr.h" |
32 | #include "gcstruct.h" |
33 | #include "pixmapstr.h" |
34 | #include "windowstr.h" |
35 | #include "mi.h" |
36 | #include "picturestr.h" |
37 | #include "mipict.h" |
38 | #include "colormapst.h" |
39 | |
40 | #define NUM_CUBE_LEVELS4 4 |
41 | #define NUM_GRAY_LEVELS13 13 |
42 | |
43 | static Bool |
44 | miBuildRenderColormap(ColormapPtr pColormap, Pixel * pixels, int *nump) |
45 | { |
46 | int r, g, b; |
47 | unsigned short red, green, blue; |
48 | Pixel pixel; |
49 | Bool used[MI_MAX_INDEXED256]; |
50 | int needed; |
51 | int policy; |
52 | int cube, gray; |
53 | int i, n; |
54 | |
55 | if (pColormap->mid != pColormap->pScreen->defColormap) { |
| |
56 | policy = PictureCmapPolicyAll4; |
57 | } |
58 | else { |
59 | int avail = pColormap->pVisual->ColormapEntries; |
60 | |
61 | policy = PictureCmapPolicy; |
62 | if (policy == PictureCmapPolicyDefault0) { |
63 | if (avail >= 256 && |
64 | (pColormap->pVisual->class | DynamicClass1) == PseudoColor3) |
65 | policy = PictureCmapPolicyColor3; |
66 | else if (avail >= 64) |
67 | policy = PictureCmapPolicyGray2; |
68 | else |
69 | policy = PictureCmapPolicyMono1; |
70 | } |
71 | } |
72 | |
73 | |
74 | |
75 | for (;;) { |
| 2 | | Loop condition is true. Entering loop body | |
|
76 | switch (policy) { |
| 3 | | Control jumps to 'case 4:' at line 77 | |
|
77 | case PictureCmapPolicyAll4: |
78 | needed = 0; |
79 | break; |
| 4 | | Execution continues on line 91 | |
|
80 | case PictureCmapPolicyColor3: |
81 | needed = 71; |
82 | break; |
83 | case PictureCmapPolicyGray2: |
84 | needed = 11; |
85 | break; |
86 | case PictureCmapPolicyMono1: |
87 | default: |
88 | needed = 0; |
89 | break; |
90 | } |
91 | if (needed <= pColormap->freeRed) |
| |
92 | break; |
| 6 | | Execution continues on line 99 | |
|
93 | policy--; |
94 | } |
95 | |
96 | |
97 | |
98 | |
99 | cube = gray = 0; |
100 | switch (policy) { |
| 7 | | Control jumps to 'case 4:' at line 101 | |
|
101 | case PictureCmapPolicyAll4: |
102 | |
103 | |
104 | |
105 | if ((pColormap->pVisual->class | DynamicClass1) == PseudoColor3) { |
| |
106 | for (cube = 1; |
| 9 | | Loop condition is false. Execution continues on line 109 | |
|
107 | cube * cube * cube < pColormap->pVisual->ColormapEntries; |
108 | cube++); |
109 | cube--; |
110 | if (cube == 1) |
| |
111 | cube = 0; |
112 | } |
113 | else |
114 | cube = 0; |
115 | |
116 | |
117 | |
118 | |
119 | if (cube) { |
| |
120 | needed = pColormap->pVisual->ColormapEntries - (cube * cube * cube); |
121 | |
122 | gray = needed / (cube - 1); |
123 | |
124 | gray = (gray + 1) * (cube - 1) + 1; |
125 | } |
126 | else |
127 | gray = pColormap->pVisual->ColormapEntries; |
128 | break; |
| 12 | | Execution continues on line 142 | |
|
129 | |
130 | case PictureCmapPolicyColor3: |
131 | cube = NUM_CUBE_LEVELS4; |
132 | |
133 | case PictureCmapPolicyGray2: |
134 | gray = NUM_GRAY_LEVELS13; |
135 | break; |
136 | case PictureCmapPolicyMono1: |
137 | default: |
138 | gray = 2; |
139 | break; |
140 | } |
141 | |
142 | memset(used, '\0', pColormap->pVisual->ColormapEntries * sizeof(Bool))__builtin___memset_chk (used, '\0', pColormap->pVisual-> ColormapEntries * sizeof(Bool), __builtin_object_size (used, 0 )); |
143 | for (r = 0; r < cube; r++) |
| 13 | | Loop condition is false. Execution continues on line 155 | |
|
144 | for (g = 0; g < cube; g++) |
145 | for (b = 0; b < cube; b++) { |
146 | pixel = 0; |
147 | red = (r * 65535 + (cube - 1) / 2) / (cube - 1); |
148 | green = (g * 65535 + (cube - 1) / 2) / (cube - 1); |
149 | blue = (b * 65535 + (cube - 1) / 2) / (cube - 1); |
150 | if (AllocColor(pColormap, &red, &green, |
151 | &blue, &pixel, 0) != Success0) |
152 | return FALSE0; |
153 | used[pixel] = TRUE1; |
154 | } |
155 | for (g = 0; g < gray; g++) { |
| 14 | | Assuming 'g' is < 'gray' | |
|
| 15 | | Loop condition is true. Entering loop body | |
|
156 | pixel = 0; |
157 | red = green = blue = (g * 65535 + (gray - 1) / 2) / (gray - 1); |
| |
158 | if (AllocColor(pColormap, &red, &green, &blue, &pixel, 0) != Success0) |
159 | return FALSE0; |
160 | used[pixel] = TRUE1; |
161 | } |
162 | n = 0; |
163 | for (i = 0; i < pColormap->pVisual->ColormapEntries; i++) |
164 | if (used[i]) |
165 | pixels[n++] = i; |
166 | |
167 | *nump = n; |
168 | |
169 | return TRUE1; |
170 | } |
171 | |
172 | |
173 | static Pixel |
174 | FindBestColor(miIndexedPtr pIndexed, Pixel * pixels, int num, |
175 | int red, int green, int blue) |
176 | { |
177 | Pixel best = pixels[0]; |
178 | int bestDist = 1 << 30; |
179 | int dist; |
180 | int dr, dg, db; |
181 | |
182 | while (num--) { |
183 | Pixel pixel = *pixels++; |
184 | CARD32 v = pIndexed->rgba[pixel]; |
185 | |
186 | dr = ((v >> 19) & 0x1f); |
187 | dg = ((v >> 11) & 0x1f); |
188 | db = ((v >> 3) & 0x1f); |
189 | dr = dr - red; |
190 | dg = dg - green; |
191 | db = db - blue; |
192 | dist = dr * dr + dg * dg + db * db; |
193 | if (dist < bestDist) { |
194 | bestDist = dist; |
195 | best = pixel; |
196 | } |
197 | } |
198 | return best; |
199 | } |
200 | |
201 | |
202 | static Pixel |
203 | FindBestGray(miIndexedPtr pIndexed, Pixel * pixels, int num, int gray) |
204 | { |
205 | Pixel best = pixels[0]; |
206 | int bestDist = 1 << 30; |
207 | int dist; |
208 | int dr; |
209 | int r; |
210 | |
211 | while (num--) { |
212 | Pixel pixel = *pixels++; |
213 | CARD32 v = pIndexed->rgba[pixel]; |
214 | |
215 | r = v & 0xff; |
216 | r = r | (r << 8); |
217 | dr = gray - (r >> 1); |
218 | dist = dr * dr; |
219 | if (dist < bestDist) { |
220 | bestDist = dist; |
221 | best = pixel; |
222 | } |
223 | } |
224 | return best; |
225 | } |
226 | |
227 | Bool |
228 | miInitIndexed(ScreenPtr pScreen, PictFormatPtr pFormat) |
229 | { |
230 | ColormapPtr pColormap = pFormat->index.pColormap; |
231 | VisualPtr pVisual = pColormap->pVisual; |
232 | miIndexedPtr pIndexed; |
233 | Pixel pixels[MI_MAX_INDEXED256]; |
234 | xrgb rgb[MI_MAX_INDEXED256]; |
235 | int num; |
236 | int i; |
237 | Pixel p, r, g, b; |
238 | |
239 | if (pVisual->ColormapEntries > MI_MAX_INDEXED256) |
240 | return FALSE0; |
241 | |
242 | if (pVisual->class & DynamicClass1) { |
243 | if (!miBuildRenderColormap(pColormap, pixels, &num)) |
244 | return FALSE0; |
245 | } |
246 | else { |
247 | num = pVisual->ColormapEntries; |
248 | for (p = 0; p < num; p++) |
249 | pixels[p] = p; |
250 | } |
251 | |
252 | pIndexed = malloc(sizeof(miIndexedRec)); |
253 | if (!pIndexed) |
254 | return FALSE0; |
255 | |
256 | pFormat->index.nvalues = num; |
257 | pFormat->index.pValues = malloc(num * sizeof(xIndexValue)); |
258 | if (!pFormat->index.pValues) { |
259 | free(pIndexed); |
260 | return FALSE0; |
261 | } |
262 | |
263 | |
264 | |
265 | |
266 | QueryColors(pColormap, num, pixels, rgb, serverClient); |
267 | for (i = 0; i < num; i++) { |
268 | p = pixels[i]; |
269 | pFormat->index.pValues[i].pixel = p; |
270 | pFormat->index.pValues[i].red = rgb[i].red; |
271 | pFormat->index.pValues[i].green = rgb[i].green; |
272 | pFormat->index.pValues[i].blue = rgb[i].blue; |
273 | pFormat->index.pValues[i].alpha = 0xffff; |
274 | pIndexed->rgba[p] = (0xff000000 | |
275 | ((rgb[i].red & 0xff00) << 8) | |
276 | ((rgb[i].green & 0xff00)) | |
277 | ((rgb[i].blue & 0xff00) >> 8)); |
278 | } |
279 | |
280 | |
281 | |
282 | |
283 | |
284 | switch (pVisual->class | DynamicClass1) { |
285 | case GrayScale1: |
286 | pIndexed->color = FALSE0; |
287 | for (r = 0; r < 32768; r++) |
288 | pIndexed->ent[r] = FindBestGray(pIndexed, pixels, num, r); |
289 | break; |
290 | case PseudoColor3: |
291 | pIndexed->color = TRUE1; |
292 | p = 0; |
293 | for (r = 0; r < 32; r++) |
294 | for (g = 0; g < 32; g++) |
295 | for (b = 0; b < 32; b++) { |
296 | pIndexed->ent[p] = FindBestColor(pIndexed, pixels, num, |
297 | r, g, b); |
298 | p++; |
299 | } |
300 | break; |
301 | } |
302 | pFormat->index.devPrivate = pIndexed; |
303 | return TRUE1; |
304 | } |
305 | |
306 | void |
307 | miCloseIndexed(ScreenPtr pScreen, PictFormatPtr pFormat) |
308 | { |
309 | free(pFormat->index.devPrivate); |
310 | pFormat->index.devPrivate = NULL((void*)0); |
311 | free(pFormat->index.pValues); |
312 | pFormat->index.pValues = NULL((void*)0); |
313 | } |
314 | |
315 | void |
316 | miUpdateIndexed(ScreenPtr pScreen, |
317 | PictFormatPtr pFormat, int ndef, xColorItem * pdef) |
318 | { |
319 | miIndexedPtr pIndexed = pFormat->index.devPrivate; |
320 | |
321 | if (pIndexed) { |
322 | while (ndef--) { |
323 | pIndexed->rgba[pdef->pixel] = (0xff000000 | |
324 | ((pdef->red & 0xff00) << 8) | |
325 | ((pdef->green & 0xff00)) | |
326 | ((pdef->blue & 0xff00) >> 8)); |
327 | pdef++; |
328 | } |
329 | } |
330 | } |
331 | |
332 | #endif /* _MIINDEX_H_ */ |