File: testpattern.c

package info (click to toggle)
libggidemos 990518-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 648 kB
  • ctags: 310
  • sloc: ansic: 4,015; perl: 2,540; makefile: 17
file content (358 lines) | stat: -rw-r--r-- 9,471 bytes parent folder | download
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
/*
	test pattern generator
	**********************
	$Id: testpattern.c,v 1.2 1999/01/21 07:28:33 ajapted Exp $
	(c) H. Niemann  1997,1998

 *   This software is placed in the public domain and can be used freely
 *   for any purpose. It comes without any kind of warranty, either
 *   expressed or implied, including, but not limited to the implied
 *   warranties of merchantability or fitness for a particular purpose.
 *   Use it at your own risk. the author is not responsible for any damage
 *   or consequences raised by use or inability to use this program.
 
	This program creates a test pattern like ctmon3 and the
	Nokia montest program created or is/was broadcast on tv
	during program breaks, when there was no 24h program.

	for a first glance: run
		testpattern 
	or
		testpattern 3 600x300

	Very much is copied from ./demo.c
	The text output would require about 400x300 dots, but it works
	with less too.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

#include <ggi/ggi.h>


ggi_visual_t vis;

ggi_uint white, black, red, green, blue, yellow, magenta, cyan;  

void usage(const char *prog)
{
	fprintf(stderr,"Usage:  %s [timeout [mode]]\n"
		"\ttimeout: wait <x> seconds;\n"
		"\t\t0: wait for keypress (default)\n"
		"\tmode: ggi mode string, like 640x490#800x900\n",prog);
	exit(1);
}

void stripevert(int x1,int y1,int x2,int y2,ggi_uint col1, ggi_uint col2, ggi_uint s)
{
	int x,xi;
	assert(s>0);

	ggiSetGCForeground(vis,col1);
	ggiDrawBox(vis,x1,y1,x2-x1+1,y2-y1+1);

	ggiSetGCForeground(vis,col2);
	for (x=x1; x<=x2;x+=s+s)
		for (xi=0; xi<s;xi++)
			ggiDrawVLine(vis,x+xi,y1,y2-y1+1);
}

void setcolors(void)
{
	/* just set the needed colour names */
	ggi_color col;
	ggi_mode mode;

	ggiGetMode(vis, &mode);

	/* If we're in indexed mode, the initial palette is undefined */
	if(GT_SCHEME(mode.graphtype)==GT_PALETTE) {
		ggiSetColorfulPalette(vis);
	}

	col.r=0xFFFF; col.g=0xFFFF; col.b=0xFFFF;
	white=ggiMapColor(vis, &col);

	col.r=0xFFFF; col.g=0xFFFF; col.b=0x0000;
	yellow=ggiMapColor(vis, &col);

	col.r=0xFFFF; col.g=0x0000; col.b=0xFFFF;
	magenta=ggiMapColor(vis, &col);

	col.r=0xFFFF; col.g=0x0000; col.b=0x0000;
	red=ggiMapColor(vis, &col);

	col.r=0x0000; col.g=0xFFFF; col.b=0xFFFF;
	cyan=ggiMapColor(vis, &col);

	col.r=0x0000; col.g=0xFFFF; col.b=0x0000;
	green=ggiMapColor(vis, &col);

	col.r=0x0000; col.g=0x0000; col.b=0xFFFF;
	blue=ggiMapColor(vis, &col);

	col.r=0x0000; col.g=0x0000; col.b=0x0000;
	black=ggiMapColor(vis, &col);
}

int circle(ggi_visual_t vis,int xcenter,int ycenter,int radius)
{
        /* easiest implementation, not time-optimised on weird archs. */
        int x,y,od,md,sd;

        /* original clipping code deleted. Leave clipping to ggiDrawPixel */
        /* shouldn't clip in this application anyway. */

        x=radius;y=md=0;
        while(x>=y) {
          ggiDrawPixel(vis,xcenter-y,ycenter-x);
          ggiDrawPixel(vis,xcenter+y,ycenter-x);
          ggiDrawPixel(vis,xcenter+x,ycenter-y);
          ggiDrawPixel(vis,xcenter-x,ycenter-y);
          ggiDrawPixel(vis,xcenter-x,ycenter+y);
          ggiDrawPixel(vis,xcenter+x,ycenter+y);
          ggiDrawPixel(vis,xcenter+y,ycenter+x);
          ggiDrawPixel(vis,xcenter-y,ycenter+x);
          od=md+y+y+1;sd=od-x-x-1;y++;md=od;
          if (abs(sd)<abs(od)) {x--;md=sd;}
        }

        return(0);
}

void testpattern(void)
{

#define PARTSHOR 16
#define PARTSVERT 12
#define PARTX(x) ((x)*(xmax-1)/PARTSHOR)
#define PARTWIDTH (xmax/PARTSHOR)
#define PARTY(y) ((y)*(ymax-1)/PARTSVERT)
#define PARTHEIGHT (ymax/PARTSVERT)
#define MIDX PARTX(PARTSHOR/2)
#define MIDY PARTY(PARTSVERT/2)

#define ggiDrawCircle(v,x,y,r) circle(v,x,y,r)

	int xmax, ymax;
	ggi_color col;
	int i;
	ggi_mode currmode;

	int FONTX, FONTY; /* used to be #defines */

	int xleft;
	int xright;
	int reddish=2,bluish=3,greenish=4,grey=5;
	int x,c;

	char s[255];

	ggiGetMode(vis,&currmode);
	xmax = currmode.visible.x;
	ymax = currmode.visible.y;

	ggiGetCharSize(vis, &FONTX, &FONTY);

	ggiSetGCForeground(vis,black);
	ggiFillscreen(vis);
	ggiSetGCForeground(vis,white);

	/* Grid */
	for (i=0;i<=PARTSHOR;i++) 
		ggiDrawVLine(vis,PARTX(i),0,ymax);
	for (i=0;i<=PARTSVERT;i++) {
		ggiDrawHLine(vis,0,PARTY(i),xmax);
	}

	ggiDrawCircle(vis,PARTX(1),PARTY(1),PARTHEIGHT);
	ggiDrawCircle(vis,PARTX(PARTSHOR-1),PARTY(1),PARTHEIGHT);
	ggiDrawCircle(vis,PARTX(1),PARTY(PARTSVERT-1),PARTHEIGHT);
	ggiDrawCircle(vis,PARTX(PARTSHOR-1),PARTY(PARTSVERT-1),PARTHEIGHT);
	ggiDrawCircle(vis,PARTX(PARTSHOR/2),PARTY(PARTSVERT/2),/*ymax/2*/
			(PARTY(PARTSVERT-1)-PARTY(1))/2);

	ggiSetGCForeground(vis,white);
	ggiDrawBox(vis,PARTX(PARTSHOR/2-4),PARTY(3)+1,
		   PARTX(PARTSHOR/2-3)-PARTX(PARTSHOR/2-4),
		   PARTY(5)-PARTY(3)-1);
	ggiSetGCForeground(vis,yellow);
	ggiDrawBox(vis,PARTX(PARTSHOR/2-3),PARTY(3)+1,
		   PARTX(PARTSHOR/2-2)-PARTX(PARTSHOR/2-3),
		   PARTY(5)-PARTY(3)-1);
	ggiSetGCForeground(vis,red);
	ggiDrawBox(vis,PARTX(PARTSHOR/2-2),PARTY(3)+1,
		   PARTX(PARTSHOR/2-1)-PARTX(PARTSHOR/2-2),
		   PARTY(5)-PARTY(3)-1);
	ggiSetGCForeground(vis,green);
	ggiDrawBox(vis,PARTX(PARTSHOR/2-1),PARTY(3)+1,
		   PARTX(PARTSHOR/2-0)-PARTX(PARTSHOR/2-1),
		   PARTY(5)-PARTY(3)-1);
	ggiSetGCForeground(vis,cyan);
	ggiDrawBox(vis,PARTX(PARTSHOR/2+0),PARTY(3)+1,
		   PARTX(PARTSHOR/2+1)-PARTX(PARTSHOR/2-0),
		   PARTY(5)-PARTY(3)-1);
	ggiSetGCForeground(vis,blue);
	ggiDrawBox(vis,PARTX(PARTSHOR/2+1),PARTY(3)+1,
		   PARTX(PARTSHOR/2+2)-PARTX(PARTSHOR/2+1),
		   PARTY(5)-PARTY(3)-1);
	ggiSetGCForeground(vis,magenta);
	ggiDrawBox(vis,PARTX(PARTSHOR/2+2),PARTY(3)+1,
		   PARTX(PARTSHOR/2+3)-PARTX(PARTSHOR/2+2),
		   PARTY(5)-PARTY(3)-1);
	ggiSetGCForeground(vis,black);
	ggiDrawBox(vis,PARTX(PARTSHOR/2+3),PARTY(3)+1,
		   PARTX(PARTSHOR/2+4)-PARTX(PARTSHOR/2+3),
		   PARTY(5)-PARTY(3)-1);
	
	xleft=PARTX(PARTSHOR/2-4)+1;
	xright=PARTX(PARTSHOR/2+4)-1;

	for (x=xleft;x<=xright;x++){
		c=((xright-x)*0xFFFF)/(xright-xleft);
		col.r=c; col.g=c; col.b=c;
		grey=ggiMapColor(vis, &col);
		col.r=c; col.g=0; col.b=0;
		reddish=ggiMapColor(vis, &col);
		col.r=0; col.g=c; col.b=0;
		greenish=ggiMapColor(vis, &col);
		col.r=0; col.g=0; col.b=c;
		bluish=ggiMapColor(vis, &col);
		ggiSetGCForeground(vis,reddish);
		ggiDrawVLine(vis,x,PARTY(PARTSVERT-5)+1,PARTHEIGHT/2);
		ggiSetGCForeground(vis,bluish);
		ggiDrawVLine(vis,x,PARTY(PARTSVERT-5)+1+PARTHEIGHT/2 ,
			     PARTHEIGHT/2);
		ggiSetGCForeground(vis,greenish);
		ggiDrawVLine(vis,x,PARTY(PARTSVERT-4)+1,PARTHEIGHT/2);
		ggiSetGCForeground(vis,grey);
		ggiDrawVLine(vis,x,PARTY(PARTSVERT-4)+1+PARTHEIGHT/2 ,
			     PARTHEIGHT/2);
	}

	stripevert(PARTX(0)+1,PARTY(0)+1,
		   PARTX(1)-1,PARTY(1)-1,black,white,1);
	stripevert(PARTX(PARTSHOR-1)+1,PARTY(0)+1,
		   PARTX(PARTSHOR)-1,PARTY(1)-1,black,white,1);
	stripevert(PARTX(0)+1,PARTY(PARTSVERT-1)+1,
		   PARTX(1)-1,PARTY(PARTSVERT)-1,black,white,1);
	stripevert(PARTX(PARTSHOR-1)+1,PARTY(PARTSVERT-1)+1,
		   PARTX(PARTSHOR)-1,PARTY(PARTSVERT)-1,black,white,1);
	
	stripevert(PARTX(PARTSHOR/2-4)+1,PARTY(PARTSVERT/2-1)+1,
		   PARTX(PARTSHOR/2-3)-1,PARTY(PARTSVERT/2)-1,black,white,1);
	stripevert(PARTX(PARTSHOR/2-4)+1,PARTY(PARTSVERT/2)+1,
		   PARTX(PARTSHOR/2-3)-1,PARTY(PARTSVERT/2+1)-1,black,red,1);
	stripevert(PARTX(PARTSHOR/2+3)+1,PARTY(PARTSVERT/2-1)+1,
		   PARTX(PARTSHOR/2+4)-1,PARTY(PARTSVERT/2)-1,black,green,1);
	stripevert(PARTX(PARTSHOR/2+3)+1,PARTY(PARTSVERT/2)+1,
		   PARTX(PARTSHOR/2+4)-1,PARTY(PARTSVERT/2+1)-1,black,blue,1);

	
	/* text output: */
	ggiSetGCForeground(vis,black);

	ggiDrawBox(vis,MIDX- 9*FONTX,MIDY-4*FONTY,18*FONTX,8*FONTY);


	ggiSetGCForeground(vis,white);
	ggiSetGCBackground(vis,black);


	ggiPuts(vis,MIDX-(15*FONTX)/2,MIDY-2*FONTY,"GGI screen test");

	/* -visible resolution */
	sprintf(s,"size: %4dx%3d",currmode.visible.x,currmode.visible.y);
	ggiPuts(vis,MIDX-(14*FONTX)/2,MIDY-0*FONTY,s);

	/* -virtual resolution */
	sprintf(s,"virt: %4dx%3d",currmode.virt.x,currmode.virt.y);
	ggiPuts(vis,MIDX-(14*FONTX)/2,MIDY+2*FONTY,s);


	ggiFlush(vis);
}

int main(int argc,char **argv)
{
	const char *prog=argv[0];
	
	int err;
	ggi_mode mo;
	int timeout=0;


	if (argc==1){
		
		/*mo=ggiDefaultMode; gives the default, I wish it existed */
		mo.virt.x=mo.virt.y=mo.visible.x=mo.visible.y=GGI_AUTO;
		mo.frames = GGI_AUTO; mo.graphtype=GT_AUTO;mo.dpp.x=mo.dpp.y=1;
 
	} else if (argc==2) {
		if (argv[1][0]=='-'){
			usage(prog);
			exit(1);
		}
		timeout=atoi(argv[1]); 
       
                /*mo=ggiDefaultMode; gives the default, I wish it existed */
		mo.virt.x=mo.virt.y=mo.visible.x=mo.visible.y=GGI_AUTO;
		mo.frames = GGI_AUTO; mo.graphtype=GT_AUTO;mo.dpp.x=mo.dpp.y=1;
 
	} else if (argc==3) {
		timeout=atoi(argv[1]); 
		ggiParseMode(argv[2],&mo);
	} else {
		usage(prog);
	}

	if (ggiInit() != 0) {
		fprintf(stderr, "%s: unable to initialize libggi, exiting.\n",
			argv[0]);
		exit(1);
	}
	if ((vis=ggiOpen(NULL)) == NULL) {
		fprintf(stderr,
			"%s: unable to open default visual, exiting.\n",
			argv[0]);
		exit(1);
	}
	ggiSetFlags(vis, GGIFLAG_ASYNC);


	printf("Trying mode ");
	ggiPrintMode(&mo);
	printf("\n");

	ggiCheckMode(vis,&mo);

	printf("Suggested mode ");
	ggiPrintMode(&mo);
	printf("\n");

	err=ggiSetMode(vis,&mo);

	if (err) {
		fprintf(stderr,"Can't set mode\n");
		return 2;
	}

	setcolors();

	testpattern();

	if (timeout <=0){
		ggiGetc(vis);
	} else {
		sleep((unsigned int)timeout);
	}


	ggiClose(vis);

	ggiExit();	

	return 0;
}