summaryrefslogtreecommitdiff
path: root/img3d.c
blob: 29c9b10fe08cd0dbd4ca3a2ef5df210e68d6b62d (plain)
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
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef unsigned long ulong;

#define SIZEX		400
#define SIZEX2		(SIZEX/2)
#define SIZEY		400
#define SIZEY2		(SIZEY/2)

static int SCANSCALE	=64;

static Display		*display;
static Window		window,root;
static XImage		*src,*dest;
static XEvent		ev;
static ulong		black,white;
static int		width,height,cx,cy;
static int		shrink=False;
static GC		gc;

int main(int argc, char *argv[])
{
    void ScaleImg(XImage *dest, XImage *src);
    char str[180];
    Window rw,cw;
    int rx,ry,cx,cy,bmask;
    int f;

    if (argc>1)
	if (!strcmp(argv[1],"-help"))
	{
	    fprintf(stderr,"Keys :\n");
	    fprintf(stderr,"LMB -  Lower viewing angle\n");
	    fprintf(stderr,"MMB -  Raise viewing angle\n");
	    fprintf(stderr,"RMB -  Quit\n");
	}

    if (!(display=XOpenDisplay(NULL)))
    {
	fprintf(stderr,"%s: couldn't open display\n",argv[0]);
	exit(1);
    }

    root=DefaultRootWindow(display);
    width=DisplayWidth(display,DefaultScreen(display));
    height=DisplayHeight(display,DefaultScreen(display));

    window=XCreateSimpleWindow (display,root,0,800,SIZEX,SIZEY,0,0,0);

    gc=XCreateGC(display,window,0L,NULL);

    XSetBackground(display,gc,white=WhitePixel(display,0));
    XSetForeground(display,gc,black=BlackPixel(display,0));
    XSetPlaneMask(display,gc,AllPlanes);

    XSelectInput (display,window,ButtonPressMask);
    XMapWindow(display,window);

    while(True)
    {
	if (XCheckTypedWindowEvent(display,window,ButtonPress,&ev))
	    switch(ev.xbutton.button)
	    {
		case 1:
		    SCANSCALE=SCANSCALE>>1;

		    if (SCANSCALE<1)
			SCANSCALE=1;
		    break;

		case 2:
		    SCANSCALE=SCANSCALE<<1;
		    break;

		case 3:
		    XCloseDisplay(display);
		    exit(0);
		    break;
	    }

	XQueryPointer(display,window,&rw,&cw,&rx,&ry,&cx,&cy,&bmask);

	rx-=SIZEX/2;
	ry-=SIZEY/2;

	if (rx<0)
	    rx=0;

	if (ry<0)
	    ry=0;

	if ((rx+SIZEX)>=width)
	    rx=width-SIZEX;

	if ((ry+SIZEY)>=height)
	    ry=height-SIZEY;

	src=XGetImage(display,root,rx,ry,SIZEX,SIZEY,AllPlanes,ZPixmap);

	/* Cheat to create an image we know will be the same as the source...
	*/
	dest=XGetImage(display,root,0,0,SIZEX,SIZEY,AllPlanes,ZPixmap);

	ScaleImg(src,dest);

	XPutImage(display,window,gc,dest,0,0,0,0,SIZEX,SIZEY);

	XDestroyImage (src);
	XDestroyImage (dest);
	/* XSync(display,False); */
    }
}


int ScaleX (double n, double sc)
{
    return SIZEX2-(int)((SIZEX2-n)/sc);
}


int ScaleY (double n, double sc)
{
    return SIZEY2-(int)((SIZEY2-n)/sc);
}


void ScaleImg(XImage *src, XImage *dest)
{
    int x,y;
    double sc;

    sc=1.0;

    for(y=0;y<SIZEY;y++)
    {
	for(x=0;x<SIZEX;x++)
		if (sc==1)
		    XPutPixel(dest,x,y,XGetPixel(src,x,y));
		else
		    XPutPixel(dest,x,y,XGetPixel(src,ScaleX((double)x,sc),
						 ScaleY((double)y,sc)));

	sc+=(1.0/SCANSCALE);
    }
}