This file is indexed.

/usr/lib/pike7.8/modules/Protocols.pmod/X.pmod/XImage.pmod is in pike7.8-image 7.8.866-5build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* XImage.pmod
 *
 * $Id: 5d8258da2d64c8902284309c60e3c72c121c8b07 $
 */

/*
 *    Protocols.X, a Pike interface to the X Window System
 *
 *    See COPYRIGHT for copyright information.
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

#pike __REAL_VERSION__

// Image --> X module.
// Needs: Pike 0.6


// Base class.
// FIXME: Why not inherit Image.Image directly?
// Guess five times... /Per
class Image_wrapper
{
  class funcall
  {
    function f;
    
    mixed `()( mixed ... args )
    {
      mixed q = f( @args );
      if(objectp(q)) set_image( q );
      return q;
    }
    
    void create(function q) { f = q; }
  }
  
  object image; // The real image.

  void clear_caches(int x, int y, int width, int height)
  {
    // Changed by the inheriting class.
  }

  void redraw(int x, int y, int width, int height)
  {
    // Changed by the inheriting class.
  }

  object copy(mixed ... args)
  {
    return image->copy( @args );
  }

  object paste(object what, int|void x, int|void y)
  {
    image->paste(what, x, y);
    clear_caches(x,y,what->xsize(),what->ysize());
    redraw(x,y,what->xsize(),what->ysize());
  }

  object paste_mask(object what, object mask, int|void x, int|void y)
  {
    image->paste_mask(what, mask, x, y);
    clear_caches(x,y,what->xsize(),what->ysize());
    redraw(x,y,what->xsize(),what->ysize());
  }

  void set_image(object to, int|void no_redraw)
  {
    image = to;
    clear_caches(0,0,image->xsize(),image->ysize());
    if (!no_redraw)
      redraw(0,0,image->xsize(),image->ysize());
  }

  mixed `->( string ind )
  {
    mixed x;
    if((x = `[](this,ind)))
      return x;
    return funcall(image[ind]);
  }
  
}

class XImage
{
  inherit Image_wrapper;
  import Protocols.X;

  object /*(Types.Window)*/ window;
  object /*(Types.RootWindow)*/ root; // extends window
  object /*(Types.Visual)*/ visual;
  object /*(Types.Colormap)*/ colormap;
  Image.Colortable ccol;
  object /*(Types.GC)*/ dgc;

  int best;

  int depth, bpp;
  function converter;
  int linepad, swapbytes;
  int rmask, gmask, bmask;

  int offset_x, offset_y;
  
  void set_render(string type)
  {
    if(type == "best") best=1;
  }

  Image.Colortable allocate_colortable()
  {
//     werror("Allocating colortable\n");
    array wanted;
    if(best)
      wanted = image->select_colors( 100 );
    else
      wanted = image->select_colors( 32 );

    if(sizeof(wanted) < 10) 
    {
      object i = Image.Image(100,100);
      i->tuned_box(0,0, 100, 50,
		   ({ ({0,255,255 }),({255,255,255 }),
		      ({0,0,255 }),({255,0,255 }) }));
      i->tuned_box(0,0, 100, 50,
		   ({ ({0,0,255 }),({255,0,255 }),
		      ({0,255,255 }),({255,255,0 }) }));
      i = i->hsv_to_rgb();
      wanted += i->select_colors(100);
    }

    int max_pixel;

    Array.map(((array)wanted+({({255,255,255}),({0,0,0})})),
	    lambda(array o){
	      if (objectp(o)) o = o->rgb();
	      return colormap->AllocColor(o[0]*257,o[1]*257,o[2]*257);
	    });

    foreach(values(colormap->alloced), mapping m)
      if(m && m->pixel > max_pixel)
	max_pixel = m->pixel;

    array res = allocate( max_pixel+1 );

    foreach(values(colormap->alloced), mapping m)
      if(m) res[ m->pixel ] = ({ m->red/257, m->green/257, m->blue/257 });

    Image.Colortable ct = Image.Colortable( res );
    ct->cubicles(12, 12, 12);
    if(best)
      ct->floyd_steinberg();
    else
      ct->ordered();
//  werror(sprintf("colortable: %O\n", res));
    return ct;
  }
  
  void clear_caches(int x, int y, int width, int height)
  {
    // no inteligence yet...
  }

  void redraw(int x, int y, int width, int height)
  {
    int max_pixels = ((window->display->maxRequestSize - 64)*32) / bpp;
    int slice = (max_pixels / width)-1;

//     werror(sprintf("XImage->redraw: [%d, %d]+[%d, %d] (%d)\n",
// 		   x, y, width, height, slice));

    if(x+width > image->xsize())
      width = image->xsize()-x;
    if (x<0)
    {
      width += x;
      x = 0;
    }
    if(width<=0) return;

    if(y+height > image->ysize())
      height = image->ysize()-y;
    if (y<0)
    {
      height += y;
      y = 0;
    }

    if(height<=0) return;

    while(height>0)
    {
      height -= slice;
      if(height < 0) slice += height;
      object mimg = image->copy(x,y,x+width-1,y+slice-1);
      if(rmask)
      {
	string data = 
	  converter(mimg,bpp,linepad,swapbytes,rmask,gmask,bmask,
		    @(ccol?({ccol}):({})));

	window->PutImage( dgc, depth, x + offset_x, y + offset_y,
			  width, slice, data, 2 );
      }
      else
      {
	if(!ccol) ccol = allocate_colortable();
	string data = converter( mimg, bpp, linepad, depth, ccol );
	window->PutImage( dgc, bpp, x + offset_x, y + offset_y,
			  width, slice, data, 2 );
      }
      y += slice;
    }
  }

  void set_window(object w)
  {
    set_drawable(w);
  }

  void set_drawable(object w)
  {
    window = w;
    if(!w->visual)
    {
      object q = w->parent;
      if(q)
	do {
	  if(q->visual)
	  {
	    visual = q->visual;
	    break;
	  } else {
	    q = q->parent;
	  }
	} while(q);
      q = w;
      while(q) { root = q; q = q->parent; }

      if(!visual)
	visual = root->visual;
      root = w->root;
    } else {
      root = w->parent;
      while(root->parent) root = root->parent;
      visual = w->visual;
    }
    if(w->colormap)
      colormap = w->colormap;
    else
      colormap = root->defaultColorMap;
    
    swapbytes = !w->display->imageByteOrder;
    rmask = visual->redMask;
    gmask = visual->greenMask;
    bmask = visual->blueMask;
    depth = visual->depth;

    foreach(w->display->formats, mapping format)
      if(format->depth == depth)
      {
// 	werror(sprintf("found matching format: %O", format));
	bpp = format->bitsPerPixel;
	linepad = format->scanLinePad;
	break;
      }
    
    switch(_Xlib.visual_classes[visual->c_class])
    {
     case "StaticGray":
       ccol = Image.Colortable(0,0,0, ({0,0,0}), ({255,255,255}), 1<<depth);
       converter = Image.X.encode_pseudocolor;
       break;
//     ccol = Image.Colortable(0,0,0, ({0,0,0}), ({255,255,255}), 1<<depth);
//     converter = Image.X.encode_pseudocolor;
//     break;
     case "GrayScale":
     case "PseudoColor":
       converter = Image.X.encode_pseudocolor;
       break;
     case "StaticColor":
     case "TrueColor":
       if(depth <= 16)
       {
#define BITS(Y) (Image.X.examine_mask(Y)[0])
	 ccol = Image.Colortable(1<<BITS(rmask),
				 1<<BITS(gmask),
				 1<<BITS(bmask));
	 ccol->ordered();
       } else
	 ccol = 0;
       converter = Image.X.encode_truecolor_masks;
       break;
     case "DirectColor":
       error("Cannot handle Direct Color visual yet."); 
       break;
    }
    dgc = window->CreateGC();
  }

  void set_offset(int x, int y)
  {
    offset_x = x;
    offset_y = y;
  }
}  
  
// Steels a few callbacks from the window.

class WindowImage
{
  inherit XImage;

  void exposed(mixed event)
  {
//     werror(sprintf("%O\n", event));
#ifdef BUGGY_X
    if(!event->count)
    {
      remove_call_out(redraw);
      call_out(redraw,0.1,0,0,image->xsize(),image->ysize());
    }
#else
    redraw(event->x, event->y, event->width, event->height);
#endif
  }

  void create(object/*(Types.Window)*/ w)
  {
    set_drawable(w);
    w->set_event_callback("Expose", exposed); // internal callback...
    w->SelectInput("Exposure");
  }
}

class PixmapImage
{
  inherit XImage;
  import Protocols.X;

  void create(Types.Pixmap p, object|void ct)
  {
    if(ct) ccol = ct;
    set_drawable( p );
  }
}

object MakeShapeMask(object in, object alpha)
{
  object shape = in->CreatePixmap(alpha->xsize(),alpha->ysize(),1);
  mapping f;
  foreach(in->display->formats, f) if(f->depth == 1) break;
  shape->PutImage( shape->CreateGC(), 1, 0, 0, alpha->xsize(), alpha->ysize(), 
		   Image.X.encode_truecolor( alpha->invert(), 
					     1, f->scanLinePad,
					     !in->display->bitmapBitOrder, 
					     1, 0, 0, 0, 0, 0), 0);
  return shape;
}


object spcm;
object SimplePixmapImage(object in, object color, int|void ocol)
{
  int width = color->xsize();
  int height = color->ysize();
  if(!ocol) 
    spcm = 0;
  object bgpm = in->CreatePixmap(width, height, in->depth);
  object pi = PixmapImage( bgpm, spcm );
  pi->set_render("best");
  pi->set_image( color );
  spcm = pi->ccol;
  return bgpm;
}

void ShapedWindowImage(object in, object color, object|void alpha, 
		       int|void contour)
{
  int width = color->xsize();
  int height = color->ysize();

  object bgpm = in->CreatePixmap(width, height, in->depth);
  PixmapImage( bgpm )->set_image( color );
  in->ChangeAttributes( (["BackPixmap":bgpm ]) );
  if(alpha)
  {
    mapping f;
    object shape = in->CreatePixmap(alpha->xsize(),alpha->ysize(),1);
    foreach(in->display->formats, f) if(f->depth == 1) break;
    shape->PutImage( shape->CreateGC(), 1, 0, 0, alpha->xsize(), alpha->ysize(), 
		     Image.X.encode_truecolor( alpha->invert(), 
					       1, f->scanLinePad,
					       !in->display->bitmapBitOrder, 
					       1, 0, 0, 0, 0, 0), 0);
    in->ShapeMask("both", 0, 0, "set", shape);
    if(contour)
    {
      in->ShapeMask("bounding", -1, -1, "union", shape);
      in->ShapeMask("bounding", -1, 1, "union", shape);
      in->ShapeMask("bounding", 1, -1, "union", shape);
      in->ShapeMask("bounding", 1, 1, "union", shape);
    
      in->ShapeMask("bounding", 1, 0, "union", shape);
      in->ShapeMask("bounding", 0, 1, "union", shape);
      in->ShapeMask("bounding", -1, 0, "union", shape);
      in->ShapeMask("bounding", 0, -1, "union", shape);
    }
  }
}