File: pixmap.c

package info (click to toggle)
afterstep 2.2.12-18.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,184 kB
  • sloc: ansic: 201,695; sh: 5,894; xml: 3,721; makefile: 2,094; perl: 1,558; cpp: 811
file content (791 lines) | stat: -rw-r--r-- 22,079 bytes parent folder | download | duplicates (9)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*--------------------------------*-C-*---------------------------------*
 * File:	pixmap.c
 *----------------------------------------------------------------------*
 * Copyright (c) 1999 Ethan Fischer <allanon@crystaltokyo.com>
 * Copyright (c) 1999 Sasha Vasko   <sasha at aftercode.net>
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*
 * Originally written:
 *    1999	Sasha Vasko <sasha at aftercode.net>
 *----------------------------------------------------------------------*/

#ifdef _WIN32
#include "win32/config.h"
#else
#include "config.h"
#endif

/*#define LOCAL_DEBUG */
/* #define DO_CLOCKING */

#ifdef DO_CLOCKING
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif


#ifdef _WIN32
# include "win32/afterbase.h"
#else
# include "afterbase.h"
#endif
#include "asvisual.h"
#include "blender.h"
#include "asimage.h"
#include "imencdec.h"
#include "ximage.h"
#include "transform.h"
#include "pixmap.h"


/*#define CREATE_TRG_PIXMAP(asv,w,h) XCreatePixmap(dpy, RootWindow(dpy,DefaultScreen(dpy)), (w), (h), DefaultDepth(dpy,DefaultScreen(dpy)))*/
#define CREATE_TRG_PIXMAP(asv,w,h) create_visual_pixmap(asv,RootWindow(asv->dpy,DefaultScreen(asv->dpy)),(w),(h),0)


/****************************************************************************
 *
 * fill part of a pixmap with the root pixmap, offset properly to look
 * "transparent"
 *
 ***************************************************************************/
int
FillPixmapWithTile (Pixmap pixmap, Pixmap tile, int x, int y, int width, int height, int tile_x, int tile_y)
{
#ifndef X_DISPLAY_MISSING
	Display *dpy = get_default_asvisual()->dpy;
  if (tile != None && pixmap != None)
    {
      GC gc;
      XGCValues gcv;

      gcv.tile = tile;
      gcv.fill_style = FillTiled;
      gcv.ts_x_origin = -tile_x;
      gcv.ts_y_origin = -tile_y;
      gc = XCreateGC (dpy, tile, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
      XFillRectangle (dpy, pixmap, gc, x, y, width, height);
      XFreeGC (dpy, gc);
      return 1;
    }
#endif
  return 0;
}

Pixmap
GetRootPixmap (Atom id)
{
	Pixmap currentRootPixmap = None;
#ifndef X_DISPLAY_MISSING
	static Atom root_pmap_atom = None ; 
	Display *dpy = get_default_asvisual()->dpy;
	if (id == None)
	{
		if( root_pmap_atom == None ) 	  
  			root_pmap_atom = XInternAtom (dpy, "_XROOTPMAP_ID", True);
		id = root_pmap_atom ;
	}

    if (id != None)
    {
  		Atom act_type;
    	int act_format;
    	unsigned long nitems, bytes_after;
    	unsigned char *prop = NULL;

/*fprintf(stderr, "\n aterm GetRootPixmap(): root pixmap is set");                  */
    	if (XGetWindowProperty (  dpy, RootWindow(dpy,DefaultScreen(dpy)), id, 0, 1, False, XA_PIXMAP,
							      &act_type, &act_format, &nitems, &bytes_after,
			    				  &prop) == Success)
		{
			if (prop)
	  		{
	    		currentRootPixmap = *((Pixmap *) prop);
	    		XFree (prop);
/*fprintf(stderr, "\n aterm GetRootPixmap(): root pixmap is [%lu]", currentRootPixmap); */
		    }
		}
    }
#endif
    return currentRootPixmap;
}

#ifndef X_DISPLAY_MISSING
static int
pixmap_error_handler (Display * dpy, XErrorEvent * error)
{
#ifdef DEBUG_IMAGING
	show_error ("XError # %u, in resource %lu, Request: %d.%d",
				 error->error_code, error->resourceid, error->request_code, error->minor_code);
#endif
  return 0;
}
#endif

Pixmap
ValidatePixmap (Pixmap p, int bSetHandler, int bTransparent, unsigned int *pWidth, unsigned int *pHeight)
{
#ifndef X_DISPLAY_MISSING
	Display *dpy = get_default_asvisual()->dpy;
	int (*oldXErrorHandler) (Display *, XErrorEvent *) = NULL;
    /* we need to check if pixmap is still valid */
	Window root;
    int junk;
	unsigned int ujunk ;
	if (bSetHandler)
		oldXErrorHandler = XSetErrorHandler (pixmap_error_handler);

    if (bTransparent)
	    p = GetRootPixmap (None);
	if (!pWidth)
  		pWidth = &ujunk;
    if (!pHeight)
	    pHeight = &ujunk;

    if (p != None)
	{
  		if (!XGetGeometry (dpy, p, &root, &junk, &junk, pWidth, pHeight, &ujunk, &ujunk))
			p = None;
    }
	if(bSetHandler)
  		XSetErrorHandler (oldXErrorHandler);

	return p;
#else
	return None ;
#endif
}

int
GetRootDimensions (int *width, int *height)
{
#ifndef X_DISPLAY_MISSING
	Display *dpy = get_default_asvisual()->dpy;
	if( dpy ) 
	{	
		*height = XDisplayHeight(dpy, DefaultScreen(dpy) );
		*width = XDisplayWidth(dpy, DefaultScreen(dpy) );
	}
	return 1 ;
#else
	return 0;
#endif
}

int
GetWinPosition (Window w, int *x, int *y)
{
	return get_dpy_window_position( get_default_asvisual()->dpy, None, w, NULL, NULL, x, y );
}

ARGB32
shading2tint32(ShadingInfo * shading)
{
	if( shading && !NO_NEED_TO_SHADE(*shading))
	{
		CARD16 r16 = ((shading->tintColor.red*shading->shading / 100)>>9)&0x00FF ;
		CARD16 g16 = ((shading->tintColor.green*shading->shading / 100)>>9)&0x00FF ;
		CARD16 b16 = ((shading->tintColor.blue*shading->shading / 100)>>9)&0x00FF ;
		CARD16 a16 = ((0x0000007F*shading->shading / 100))&0x00FF ;
		return MAKE_ARGB32(a16,r16,g16,b16);
	}
	return TINT_LEAVE_SAME ;
}

Pixmap
scale_pixmap (ASVisual *asv, Pixmap src, int src_w, int src_h, int width, int height, GC gc, ARGB32 tint)
{
	Pixmap trg = None;
#ifndef X_DISPLAY_MISSING

	if (src != None)
    {
		ASImage *src_im ;
		src_im = pixmap2ximage(asv, src, 0, 0, src_w, src_h, AllPlanes, 0 );
		if( src_im ) 
		{
			if( src_w != width || src_h != height ) 
			{
				ASImage *tmp = scale_asimage( asv, src_im, width, height, 
				                              (tint != TINT_LEAVE_SAME)?ASA_ASImage:ASA_XImage, 
											  0, ASIMAGE_QUALITY_DEFAULT );
				destroy_asimage( &src_im );
				src_im = tmp;
			}
			if( src_im && tint != TINT_LEAVE_SAME )
			{
				ASImage *tinted = tile_asimage ( asv, src_im, 0, 0,
				  								 width,  height, tint,
												 ASA_XImage,
												 0, ASIMAGE_QUALITY_DEFAULT );
				destroy_asimage( &src_im );
				src_im = tinted ;
			}
			if( src_im ) 
			{
				trg	= asimage2pixmap(asv, None, src_im, gc, True);
				destroy_asimage( &src_im );
			}
		}
	}
#endif
	return trg;
}

Pixmap
ScalePixmap (Pixmap src, int src_w, int src_h, int width, int height, GC gc, ShadingInfo * shading)
{
	Pixmap p = None ;
#ifndef X_DISPLAY_MISSING
	p = scale_pixmap( get_default_asvisual(), src, src_w, src_h, width, height, gc, shading2tint32(shading) );
#endif
	return p;
}


void
copyshade_drawable_area( ASVisual *asv, Drawable src, Pixmap trg,
				  		 int x, int y, int w, int h,
				  		 int trg_x, int trg_y,
				  		 GC gc, ARGB32 tint) 		
{
#ifndef X_DISPLAY_MISSING
	Display *dpy = get_default_asvisual()->dpy;
	if( tint == TINT_LEAVE_SAME || asv == NULL )
	{
		XCopyArea (dpy, src, trg, gc, x, y, w, h, trg_x, trg_y);
	}else
	{
		ASImage *src_im = pixmap2ximage( asv, src, x, y, w, h, AllPlanes, 0 );
		if( src_im )
		{
			ASImage *tinted = tile_asimage ( asv, src_im, 0, 0,
		  									 w,  h, tint,
											 ASA_XImage,
											 0, ASIMAGE_QUALITY_DEFAULT );
			destroy_asimage( &src_im );
			if( tinted ) 
			{
				asimage2drawable( asv, trg, tinted, gc,
                				  0, 0, trg_x, trg_y,
        						  w, h, True );
				destroy_asimage( &tinted );
			}
		}		
	}
#endif
}

void
CopyAndShadeArea ( Drawable src, Pixmap trg,
				   int x, int y, int w, int h,
				   int trg_x, int trg_y,
				   GC gc, ShadingInfo * shading)
{
#ifndef X_DISPLAY_MISSING
	Display *dpy = get_default_asvisual()->dpy;
	ARGB32 tint = shading2tint32( shading );

    if (x < 0 || y < 0)
		return;

	if( tint == TINT_LEAVE_SAME )
	{
		XCopyArea (dpy, src, trg, gc, x, y, w, h, trg_x, trg_y);
	}else
	{
		copyshade_drawable_area( get_default_asvisual(), src, trg, x, y, w, h, trg_x, trg_y, gc, tint );
	}
#endif
}

void
tile_pixmap (ASVisual *asv, Pixmap src, Pixmap trg, int src_w, int src_h, int x, int y, int w, int h, GC gc, ARGB32 tint)
{
#ifndef X_DISPLAY_MISSING
	int tile_x, tile_y, left_w, bott_h;
  
    tile_x = x % src_w;
	tile_y = y % src_h;
	left_w = MIN (src_w - tile_x, w);
	bott_h = MIN (src_h - tile_y, h);

/*fprintf( stderr, "\nShadeTiledPixmap(): tile_x = %d, tile_y = %d, left_w = %d, bott_h = %d, SRC = %dx%d TRG=%dx%d", tile_x, tile_y, left_w, bott_h, src_w, src_h, w, h); */

	/* We don't really want to do simple tile_asimage here since if tint is notint ,
	 * then we could get by with simple XCopyArea !!! 
	 */
	copyshade_drawable_area( asv, src, trg, tile_x, tile_y, left_w, bott_h, 0, 0, gc, tint);
    if (bott_h < h)
    {				/* right-top parts */
        copyshade_drawable_area( asv, src, trg, tile_x, 0, left_w, h - bott_h, 0, bott_h, gc, tint);
    }
	if (left_w < w)
    {				/* left-bott parts */
  	    copyshade_drawable_area( asv, src, trg, 0, tile_y, w - left_w, bott_h, left_w, 0, gc, tint);
    	if (bott_h < h)		/* left-top parts */
			copyshade_drawable_area( asv, src, trg, 0, 0, w - left_w, h - bott_h, left_w, bott_h, gc, tint);
    }
#endif
}

void
ShadeTiledPixmap (Pixmap src, Pixmap trg, int src_w, int src_h, int x, int y, int w, int h, GC gc, ShadingInfo * shading)
{
#ifndef X_DISPLAY_MISSING
	ARGB32 tint = shading2tint32( shading );
	tile_pixmap (get_default_asvisual(), src, trg, src_w, src_h, x, y, w, h, gc, tint);
#endif
}

Pixmap
shade_pixmap (ASVisual *asv, Pixmap src, int x, int y, int width, int height, GC gc, ARGB32 tint)
{
#ifndef X_DISPLAY_MISSING

    Pixmap trg = CREATE_TRG_PIXMAP (asv, width, height);
  
	if (trg != None)
    	copyshade_drawable_area (asv, src, trg, x, y, width, height, 0, 0, gc, tint);
	return trg;
#else
	return None ;
#endif
}

Pixmap
ShadePixmap (Pixmap src, int x, int y, int width, int height, GC gc, ShadingInfo * shading)
{
    Pixmap trg = None;
#ifndef X_DISPLAY_MISSING
	ARGB32 tint = shading2tint32( shading );
  
	trg = CREATE_TRG_PIXMAP (get_default_asvisual(), width, height);
	if (trg != None)
    {
    	copyshade_drawable_area (get_default_asvisual(), src, trg, x, y, width, height, 0, 0, gc, tint);
    }
#endif
	return trg;
}

Pixmap
center_pixmap (ASVisual *asv, Pixmap src, int src_w, int src_h, int width, int height, GC gc, ARGB32 tint)
{
	Pixmap trg = None;
#ifndef X_DISPLAY_MISSING
	Display *dpy = get_default_asvisual()->dpy;

    int x, y, w, h, src_x = 0, src_y = 0;
	/* create target pixmap of the size of the window */
	trg = CREATE_TRG_PIXMAP (asv,width, height);
	if (trg != None)
    {
    	/* fill it with background color */
    	XFillRectangle (dpy, trg, gc, 0, 0, width, height);
    	/* place image at the center of it */
    	x = (width - src_w) >> 1;
    	y = (height - src_h) >> 1;
    	if (x < 0)
		{
			src_x -= x;
			w = MIN (width, src_w + x);
			x = 0;
		}else
			w = MIN (width, src_w);
    	if (y < 0)
		{
			src_y -= y;
			h = MIN (height, src_h + y);
			y = 0;
		}else
			h = MIN (height, src_h);

    	copyshade_drawable_area ( asv, src, trg, src_x, src_y, w, h, x, y, gc, tint);
    }
#endif
	return trg;
}

Pixmap
CenterPixmap (Pixmap src, int src_w, int src_h, int width, int height, GC gc, ShadingInfo * shading)
{
	Pixmap trg = None;
#ifndef X_DISPLAY_MISSING
	ARGB32 tint = shading2tint32( shading );
  
	trg = center_pixmap( get_default_asvisual(), src, src_w, src_h, width, height, gc, tint );
#endif		
	return trg ;	
}

Pixmap
grow_pixmap (ASVisual *asv, Pixmap src, int src_w, int src_h, int width, int height, GC gc, ARGB32 tint )
{
	Pixmap trg = None;
#ifndef X_DISPLAY_MISSING
	Display *dpy = get_default_asvisual()->dpy;

	int w, h;
	/* create target pixmap of the size of the window */
	trg = CREATE_TRG_PIXMAP (asv,width, height);
	if (trg != None)
    {
    	/* fill it with background color */
    	XFillRectangle (dpy, trg, gc, 0, 0, width, height);
    	/* place image at the center of it */
    	w = MIN (width, src_w);
    	h = MIN (height, src_h);

    	copyshade_drawable_area(asv, src, trg, 0, 0, w, h, 0, 0, gc, tint);
    }
#endif	
	return trg;
}

Pixmap
GrowPixmap (Pixmap src, int src_w, int src_h, int width, int height, GC gc, ShadingInfo * shading)
{
	Pixmap trg = None;
#ifndef X_DISPLAY_MISSING
	ARGB32 tint = shading2tint32( shading );
  
	trg = grow_pixmap( get_default_asvisual(), src, src_w, src_h, width, height, gc, tint );
#endif		
	return trg ;	
}

/****************************************************************************
 * grab a section of the screen and darken it
 ***************************************************************************/
static Pixmap
cut_pixmap ( ASVisual *asv, Pixmap src, Pixmap trg,
             int x, int y,
	  		 unsigned int src_w, unsigned int src_h,
	  		 unsigned int width, unsigned int height,
	  		 GC gc, ARGB32 tint )
{
#ifndef X_DISPLAY_MISSING
	Display *dpy = get_default_asvisual()->dpy;

	Bool my_pixmap = (trg == None )?True:False ;
	int screen_w, screen_h ;
	int w = width, h = height;
	int offset_x = 0, offset_y = 0;
	int screen = DefaultScreen(dpy);

	if (width < 2 || height < 2 )
  		return trg;

	screen_w = DisplayWidth( dpy, screen );
	screen_h = DisplayHeight( dpy, screen );

	while( x+(int)width < 0 )  x+= screen_w ;
	while( x >= screen_w )  x-= screen_w ;
	while( y+(int)height < 0 )  y+= screen_h ;
	while( y >= screen_h )  y-= screen_h ;

	if( x < 0 )
	{
  		offset_x = (-x);
  		w -= offset_x ;
  		x = 0 ;
	}
	if( y < 0 )
	{
  		offset_y = (-y) ;
  		h -= offset_y;
  		y = 0 ;
	}
	if( x+w >= screen_w )
  		w = screen_w - x ;

    if( y+height >= screen_h )
	    h = screen_h - y ;
	if( (src_w == 0 || src_h == 0) && src != None ) 
	{
		Window root;
      	unsigned int dum;
      	int dummy;
		if (!XGetGeometry (dpy, src, &root, &dummy, &dummy, &src_w, &src_h, &dum, &dum))
			src = None ;
	}	 

	if (src == None) /* we don't have root pixmap ID */
    { /* we want to create Overrideredirect window overlapping out window
         with background type of Parent Relative and then grab it */
    	XSetWindowAttributes attr ;
    	XEvent event ;
    	int tick_count = 0 ;
    	Bool grabbed = False ;
        
		attr.background_pixmap = ParentRelative ;
		attr.backing_store = Always ;
		attr.event_mask = ExposureMask ;
		attr.override_redirect = True ;
		src = create_visual_window(asv, RootWindow(dpy,screen), x, y, w, h,
	  		                0, CopyFromParent, 
			  				CWBackPixmap|CWBackingStore|CWOverrideRedirect|CWEventMask,
			  				&attr); 

		if( src == None ) return trg ;
		XGrabServer( dpy );
		grabbed = True ;
		XMapRaised( dpy, src );
		XSync(dpy, False );
		start_ticker(1);
		/* now we have to wait for our window to become mapped - waiting for Expose */
		for( tick_count = 0 ; !XCheckWindowEvent( dpy, src, ExposureMask, &event ) && tick_count < 100 ; tick_count++)
	  		wait_tick();
		if( tick_count < 100 )
		{
		    if( trg == None )    trg = CREATE_TRG_PIXMAP (asv,width, height);
	  		if (trg != None)
		    {	/* custom code to cut area, so to ungrab server ASAP */
		        if (tint != TINT_LEAVE_SAME)
	  		    {
	  				ASImage *src_im = pixmap2ximage( asv, src, 0, 0, w, h, AllPlanes, 0 );
					XDestroyWindow( dpy, src );
				    src = None ;
					XUngrabServer( dpy );
					grabbed = False ;
					if (src_im != NULL)
					{
						ASImage *tinted = tile_asimage ( asv, src_im, 0, 0,
		  												 w,  h, tint,
														 ASA_XImage,
														 0, ASIMAGE_QUALITY_DEFAULT );
						destroy_asimage( &src_im );
						asimage2drawable( asv, trg, tinted, gc,
      			        				  0, 0, offset_x, offset_y,
        								  w, h, True );
						destroy_asimage( &tinted );
					}else if( my_pixmap )
					{
		  				XFreePixmap( dpy, trg );
		  				trg = None ;
					}
				}else
		  			XCopyArea (dpy, src, trg, gc, 0, 0, w, h, offset_x, offset_y);
	  		}
        }
		if( src )
	  		XDestroyWindow( dpy, src );
		if( grabbed )
	  		XUngrabServer( dpy );
		return trg ;
    }
	/* we have root pixmap ID */
	/* find out our coordinates relative to the root window */
	if (x + w > src_w || y + h > src_h)
    {			/* tiled pixmap processing here */
  		Pixmap tmp ;
    	w = MIN (w, (int)src_w);
    	h = MIN (h, (int)src_h);

    	tmp = CREATE_TRG_PIXMAP (asv, w, h);
    	if (tmp != None)
    	{
			tile_pixmap (asv, src, tmp, src_w, src_h, x, y, w, h, gc, tint);
      		if( trg == None )
			{
          		if( (trg = CREATE_TRG_PIXMAP (asv, w+offset_x, h+offset_y)) != None )
					XCopyArea (dpy, tmp, trg, gc, 0, 0, w, h, offset_x, offset_y);
			}else
	  			FillPixmapWithTile( trg, tmp, offset_x, offset_y, width, height, 0, 0 );

			XFreePixmap( dpy, tmp );
      		return trg;
    	}
    }

	/* create target pixmap of the size of the window */
	if( trg == None )    
		trg = CREATE_TRG_PIXMAP (asv, width, height);
	if (trg != None)
    {	/* cut area */
		copyshade_drawable_area( asv, src, trg, x, y, w, h, offset_x, offset_y, gc, tint); 		
    }
	return trg;
#else
	return None ;
#endif	
}

static Pixmap
CutPixmap ( Pixmap src, Pixmap trg,
            int x, int y,
	    unsigned int src_w, unsigned int src_h,
	    unsigned int width, unsigned int height,
	    GC gc, ShadingInfo * shading)
{
	Pixmap res = None;
#ifndef X_DISPLAY_MISSING
	ARGB32 tint = shading2tint32( shading );
	res = cut_pixmap( get_default_asvisual(), src, trg, x, y, src_w, src_h, width, height, gc, tint );
#endif		
	return res ;	
}

Pixmap
cut_win_pixmap (ASVisual *asv, Window win, Drawable src, int src_w, int src_h, int width,
	  		    int height, GC gc, ARGB32 tint)
{
  int x = 0, y = 0;

  if (!get_dpy_window_position( asv->dpy, None, win, NULL, NULL, &x, &y))
	return None;

  return cut_pixmap( asv, src, None, x, y, src_w, src_h, width, height, gc, tint );
}

Pixmap
CutWinPixmap (Window win, Drawable src, int src_w, int src_h, int width,
	      int height, GC gc, ShadingInfo * shading)
{
  int x = 0, y = 0;

  if (!get_dpy_window_position( get_default_asvisual()->dpy, None, win, NULL, NULL, &x, &y))
	return None;

  return CutPixmap( src, None, x, y, src_w, src_h, width, height, gc, shading );
}

/* PROTO */
int
fill_with_darkened_background (ASVisual *asv, Pixmap * pixmap, ARGB32 tint, int x, int y, int width, int height, int root_x, int root_y, int bDiscardOriginal, ASImage *root_im)
{
#ifndef X_DISPLAY_MISSING
	unsigned int root_w, root_h;
	Pixmap root_pixmap;
	Display *dpy = get_default_asvisual()->dpy;
	int screen = DefaultScreen(dpy);

	/* added by Sasha on 02/24/1999 to use transparency&shading provided by
       libasimage 1.1 */
	root_pixmap = ValidatePixmap (None, 1, 1, &root_w, &root_h);

	if (root_pixmap != None)
    {
		if (*pixmap == None)
		{
			*pixmap = create_visual_pixmap(asv, RootWindow (dpy, screen), width, height, 0);
			bDiscardOriginal = 1;
		}
		
    	if ( tint != TINT_LEAVE_SAME)
		{
			ASImage *src_im = (root_im == NULL)?pixmap2ximage( asv, root_pixmap, 0, 0, root_w, root_h, AllPlanes, 0 ):root_im;
			if( root_im )
			{
				ASImage *tinted = tile_asimage ( asv, src_im, -root_x, -root_y,
		  										 width,  height, tint,
												 ASA_XImage,
											     0, ASIMAGE_QUALITY_DEFAULT );
				if( root_im != src_im )
					destroy_asimage( &src_im );
				if( tinted ) 
				{
					asimage2drawable( asv, *pixmap, tinted, NULL,
                					  0, 0, x, y,
        							  width, height, True );
					destroy_asimage( &tinted );
				}
			}		
		}else
	    	FillPixmapWithTile (*pixmap, root_pixmap, x, y, width, height, root_x, root_y);
        return 1;
	}
#endif
	return 0;
}

/****************************************************************************
 * grab a section of the screen and combine it with an XImage
 ***************************************************************************/
int
fill_with_pixmapped_background (ASVisual *asv, Pixmap * pixmap, ASImage *image, int x, int y, int width, int height, int root_x, int root_y, int bDiscardOriginal, ASImage *root_im)
{
#ifndef X_DISPLAY_MISSING
	unsigned int root_w, root_h;
	Pixmap root_pixmap;
	int screen = DefaultScreen(asv->dpy);

	root_pixmap = ValidatePixmap (None, 1, 1, &root_w, &root_h);
	if (root_pixmap != None)
    {
		ASImageLayer layers[2];
		ASImage *merged_im ;
		
		init_image_layers( &layers[0], 2 );
		layers[0].merge_scanlines = allanon_scanlines ;
		layers[0].im = root_im ?root_im:
		                        pixmap2ximage( asv, root_pixmap, 0, 0, root_w, root_h, AllPlanes, 0 );
		layers[0].dst_x = x ;
		layers[0].dst_y = y ;
		layers[0].clip_x = root_x ;
		layers[0].clip_y = root_y ;
		layers[0].clip_width = width ;
		layers[0].clip_height = height ;
		
		layers[1].im = image ; 
		layers[1].dst_x = x ;
		layers[1].dst_y = y ;
		layers[1].clip_x = 0 ;
		layers[1].clip_y = 0 ;
		layers[1].clip_width = width ;
		layers[1].clip_height = height ;

		merged_im = merge_layers( asv, &layers[0], 2,
		                          width, height,
								  ASA_XImage,
							      0, ASIMAGE_QUALITY_DEFAULT );
		if( root_im != layers[0].im )
			destroy_asimage( &(layers[0].im) );

		if( merged_im ) 
		{
  			if (*pixmap == None)
				*pixmap = create_visual_pixmap (asv, RootWindow (asv->dpy, screen), width, height, 0);

			asimage2drawable( asv, *pixmap, merged_im, NULL,
          					  0, 0, x, y,
  							  width, height, True );
			destroy_asimage( &merged_im );
		}			
    	return 1;
    }
#endif
	return 0;
}
/************************************************/