all repos — openbox @ 4947902d269213edee40f3f31f97721fa0dd3877

openbox fork - make it a bit more like ryudo

otk_c/imagecontrol.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// -*- mode: C; indent-tabs-mode: nil; c-basic-offset: 2; -*-

#include "../config.h"
#include "imagecontrol.h"
#include "timer.h"
#include "screeninfo.h"
#include "display.h"

typedef struct CachedImage {
  Pixmap pixmap;
  
  unsigned int count, width, height;
  unsigned long pixel1, pixel2, texture;
} CachedImage;

static void timeout(OtkImageControl *self);
static void initColors(OtkImageControl *self, Visual *visual);

PyObject *OtkImageControl_New(int screen)
{
  OtkImageControl *self;
  int count, i;
  XPixmapFormatValues *pmv;

  self = PyObject_New(OtkImageControl, &OtkImageControl_Type);

  self->screen = OtkDisplay_ScreenInfo(OBDisplay, screen);

  self->timer = (OtkTimer*)OtkTimer_New((OtkTimeoutHandler)timeout, self);
  self->timer->timeout = 300000;
  OtkTimer_Start(self->timer);
  self->cache_max = 200;
  
  self->dither = True; // default value
  self->cpc = 4; // default value

  // get the BPP from the X server
  self->bpp = 0;
  if ((pmv = XListPixmapFormats(OBDisplay->display, &count))) {
    for (i = 0; i < count; i++)
      if (pmv[i].depth == self->screen->depth) {
	self->bpp = pmv[i].bits_per_pixel;
	break;
      }
    XFree(pmv);
  }
  if (!self->bpp) self->bpp = self->screen->depth;
  if (self->bpp >= 24) self->dither = False; // don't need dither at >= 24 bpp

  self->grad_xbuffer = self->grad_ybuffer = NULL;
  self->grad_buffer_width = self->grad_buffer_height = 0;
  self->sqrt_table = NULL;

  initColors(self, self->screen->visual);

  return (PyObject*)self;
}

static void initColors(OtkImageControl *self, Visual *visual)
{
  // these are not used for !TrueColor
  self->red_offset = self->green_offset = self->blue_offset = 0;
  // these are not used for TrueColor
  self->colors = NULL;
  self->ncolors = 0;

  // figure out all our color settings based on the visual type
  switch (visual->class) {
  case TrueColor: {
    int i;
    unsigned long red_mask, green_mask, blue_mask;

    // find the offsets for each color in the visual's masks
    red_mask = visual->red_mask;
    green_mask = visual->green_mask;
    blue_mask = visual->blue_mask;

    while (! (red_mask & 1)) { self->red_offset++; red_mask >>= 1; }
    while (! (green_mask & 1)) { self->green_offset++; green_mask >>= 1; }
    while (! (blue_mask & 1)) { self->blue_offset++; blue_mask >>= 1; }

    // use the mask to determine the number of bits for each shade of color
    // so, best case, red_mask == 0xff (255), and so each bit is a different
    // shade!
    self->red_bits = 255 / red_mask;
    self->green_bits = 255 / green_mask;
    self->blue_bits = 255 / blue_mask;

    // compute color tables, based on the number of bits for each shade
    for (i = 0; i < 256; i++) {
      self->red_color_table[i] = i / self->red_bits;
      self->green_color_table[i] = i / self->green_bits;
      self->blue_color_table[i] = i / self->blue_bits;
    }
    break;
  }
/*
  case PseudoColor:
  case StaticColor: {
    ncolors = self->cpc * self->cpc * self->cpc; // cpc ^ 3

    if (ncolors > (1 << self->screen->depth)) {
      self->cpc = (1 << self->screen->depth) / 3;
      ncolors = self->cpc * self->cpc * self->cpc; // cpc ^ 3
    }

    if (self->cpc < 2 || self->ncolors > (1 << self->screen->depth)) {
      fprintf(stderr,
	      "OtkImageControl_New: invalid colormap size %d "
              "(%d/%d/%d) - reducing",
	      self->ncolors, self->cpc, self->cpc, self->cpc);

      self->cpc = (1 << self->screen->depth) / 3;
    }

    self->colors = malloc(sizeof(XColor) * self->ncolors);
    if (! self->colors) {
      fprintf(stderr, "OtkImageControl_New: error allocating colormap\n");
      exit(1);
    }

    int i = 0, ii, p, r, g, b,
      bits = 255 / (colors_per_channel - 1);

    red_bits = green_bits = blue_bits = bits;

    for (i = 0; i < 256; i++)
      red_color_table[i] = green_color_table[i] = blue_color_table[i] =
	i / bits;

    for (r = 0, i = 0; r < colors_per_channel; r++)
      for (g = 0; g < colors_per_channel; g++)
	for (b = 0; b < colors_per_channel; b++, i++) {
	  colors[i].red = (r * 0xffff) / (colors_per_channel - 1);
	  colors[i].green = (g * 0xffff) / (colors_per_channel - 1);
	  colors[i].blue = (b * 0xffff) / (colors_per_channel - 1);;
	  colors[i].flags = DoRed|DoGreen|DoBlue;
	}

    for (i = 0; i < ncolors; i++) {
      if (! XAllocColor(OBDisplay::display, colormap, &colors[i])) {
	fprintf(stderr, "couldn't alloc color %i %i %i\n",
		colors[i].red, colors[i].green, colors[i].blue);
	colors[i].flags = 0;
      } else {
	colors[i].flags = DoRed|DoGreen|DoBlue;
      }
    }

    XColor icolors[256];
    int incolors = (((1 << screen_depth) > 256) ? 256 : (1 << screen_depth));

    for (i = 0; i < incolors; i++)
      icolors[i].pixel = i;

    XQueryColors(OBDisplay::display, colormap, icolors, incolors);
    for (i = 0; i < ncolors; i++) {
      if (! colors[i].flags) {
	unsigned long chk = 0xffffffff, pixel, close = 0;

	p = 2;
	while (p--) {
	  for (ii = 0; ii < incolors; ii++) {
	    r = (colors[i].red - icolors[i].red) >> 8;
	    g = (colors[i].green - icolors[i].green) >> 8;
	    b = (colors[i].blue - icolors[i].blue) >> 8;
	    pixel = (r * r) + (g * g) + (b * b);

	    if (pixel < chk) {
	      chk = pixel;
	      close = ii;
	    }

	    colors[i].red = icolors[close].red;
	    colors[i].green = icolors[close].green;
	    colors[i].blue = icolors[close].blue;

	    if (XAllocColor(OBDisplay::display, colormap,
			    &colors[i])) {
	      colors[i].flags = DoRed|DoGreen|DoBlue;
	      break;
	    }
	  }
	}
      }
    }

    break;
  }

  case GrayScale:
  case StaticGray: {
    if (visual->c_class == StaticGray) {
      ncolors = 1 << screen_depth;
    } else {
      ncolors = colors_per_channel * colors_per_channel * colors_per_channel;

      if (ncolors > (1 << screen_depth)) {
	colors_per_channel = (1 << screen_depth) / 3;
	ncolors =
	  colors_per_channel * colors_per_channel * colors_per_channel;
      }
    }

    if (colors_per_channel < 2 || ncolors > (1 << screen_depth)) {
      fprintf(stderr,
              "BImageControl::BImageControl: invalid colormap size %d "
              "(%d/%d/%d) - reducing",
	      ncolors, colors_per_channel, colors_per_channel,
	      colors_per_channel);

      colors_per_channel = (1 << screen_depth) / 3;
    }

    colors = new XColor[ncolors];
    if (! colors) {
      fprintf(stderr,
              "BImageControl::BImageControl: error allocating colormap\n");
      exit(1);
    }

    int i = 0, ii, p, bits = 255 / (colors_per_channel - 1);
    red_bits = green_bits = blue_bits = bits;

    for (i = 0; i < 256; i++)
      red_color_table[i] = green_color_table[i] = blue_color_table[i] =
	i / bits;

    for (i = 0; i < ncolors; i++) {
      colors[i].red = (i * 0xffff) / (colors_per_channel - 1);
      colors[i].green = (i * 0xffff) / (colors_per_channel - 1);
      colors[i].blue = (i * 0xffff) / (colors_per_channel - 1);;
      colors[i].flags = DoRed|DoGreen|DoBlue;

      if (! XAllocColor(OBDisplay::display, colormap,
			&colors[i])) {
	fprintf(stderr, "couldn't alloc color %i %i %i\n",
		colors[i].red, colors[i].green, colors[i].blue);
	colors[i].flags = 0;
      } else {
	colors[i].flags = DoRed|DoGreen|DoBlue;
      }
    }

    XColor icolors[256];
    int incolors = (((1 << screen_depth) > 256) ? 256 :
		    (1 << screen_depth));

    for (i = 0; i < incolors; i++)
      icolors[i].pixel = i;

    XQueryColors(OBDisplay::display, colormap, icolors, incolors);
    for (i = 0; i < ncolors; i++) {
      if (! colors[i].flags) {
	unsigned long chk = 0xffffffff, pixel, close = 0;

	p = 2;
	while (p--) {
	  for (ii = 0; ii < incolors; ii++) {
	    int r = (colors[i].red - icolors[i].red) >> 8;
	    int g = (colors[i].green - icolors[i].green) >> 8;
	    int b = (colors[i].blue - icolors[i].blue) >> 8;
	    pixel = (r * r) + (g * g) + (b * b);

	    if (pixel < chk) {
	      chk = pixel;
	      close = ii;
	    }

	    colors[i].red = icolors[close].red;
	    colors[i].green = icolors[close].green;
	    colors[i].blue = icolors[close].blue;

	    if (XAllocColor(OBDisplay::display, colormap,
			    &colors[i])) {
	      colors[i].flags = DoRed|DoGreen|DoBlue;
	      break;
	    }
	  }
	}
      }
    }

    break;
  }
*/
  default:
    fprintf(stderr, "OtkImageControl: unsupported visual class: %d\n",
	    visual->class);
    exit(1);
  }
}


static void timeout(OtkImageControl *self)
{
  (void)self;
}



static void otkimagecontrol_dealloc(OtkImageControl* self)
{
  Py_DECREF(self->screen);
  Py_DECREF(self->timer);
  PyObject_Del((PyObject*)self);
}

PyTypeObject OtkImageControl_Type = {
  PyObject_HEAD_INIT(NULL)
  0,
  "OtkImageControl",
  sizeof(OtkImageControl),
  0,
  (destructor)otkimagecontrol_dealloc, /*tp_dealloc*/
  0,                            /*tp_print*/
  0,                            /*tp_getattr*/
  0,                            /*tp_setattr*/
  0,                            /*tp_compare*/
  0,                            /*tp_repr*/
  0,                            /*tp_as_number*/
  0,                            /*tp_as_sequence*/
  0,                            /*tp_as_mapping*/
  0,                            /*tp_hash */
};