Initial revision
[wmaker-crm.git] / wrlib / testdraw.c
bloba982e0bef0f42b4fa0d83d3faf374c4114785916
2 #include "wraster.h"
4 #include <X11/Xlib.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include <sys/time.h>
10 #include <time.h>
12 Display *dpy;
13 RContext *ctx;
14 char *ProgName;
17 void
18 testDraw()
20 RImage *img, *tile, *icon, *tmp;
21 RColor color, from, to;
22 RColor cdelta;
23 RSegment segs[20];
24 int i, x, y;
25 XSetWindowAttributes val;
26 Pixmap pix, back;
27 Window win;
29 val.background_pixel = ctx->black;
30 val.colormap = ctx->cmap;
31 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 128, 256,
32 0, ctx->depth, InputOutput, ctx->visual,
33 CWColormap|CWBackPixel, &val);
34 back = XCreatePixmap(ctx->dpy, ctx->drawable, 128, 256, ctx->depth);
36 /* Dark blue tile gradient */
37 from.red = 0x28;
38 from.green = 0x45;
39 from.blue = 0x69;
40 from.alpha = 0xff;
41 to.red = 0x08;
42 to.green = 0x24;
43 to.blue = 0x20;
44 to.alpha = 0xff;
46 /* Standard gray tile gradient */
47 /*from.red = 0xa6;
48 from.green = 0xa6;
49 from.blue = 0xb6;
50 from.alpha = 0xff;
51 to.red = 0x51;
52 to.green = 0x55;
53 to.blue = 0x61;
54 to.alpha = 0xff;*/
56 /* Make the tile, and put it as a sample in the first place */
57 tile = RRenderGradient(64, 64, &from, &to, RGRD_DIAGONAL);
58 img = RCloneImage(tile);
59 RConvertImage(ctx, img, &pix);
60 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 0);
62 /* Read the image, and combine it with the tile. Put it as a sample
63 * in the second slot, and also save a copy for later use. */
64 icon = RLoadImage(ctx, "ballot_box.xpm", 0);
65 if (!icon) {
66 puts(RErrorString);
67 exit(1);
69 RCombineArea(img, icon, 0, 0, icon->width, icon->height, 8, 8);
70 RDestroyImage(icon);
71 tmp = img;
72 RConvertImage(ctx, img, &pix);
73 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 64, 0);
75 img = RCloneImage(tile);
77 /* Draw random pixels on image */
78 for (i=0; i< 200; i++) {
79 color.red = rand()%256;
80 color.green = rand()%256;
81 color.blue = rand()%256;
82 color.alpha = 255;
83 x = rand()%64;
84 y = rand()%64;
85 RPutPixel(img, x, y, &color);
88 RConvertImage(ctx, img, &pix);
89 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 64);
91 RDestroyImage(img);
92 img = RCloneImage(tile);
94 /* Alter random pixels in image with the same amount for r/g/b */
95 for (i=0; i< 200; i++) {
96 cdelta.red = cdelta.green = cdelta.blue = rand()%511 - 255;
97 cdelta.alpha = 0;
98 x = rand()%64;
99 y = rand()%64;
100 ROperatePixel(img, RAddOperation, x, y, &cdelta);
103 RConvertImage(ctx, img, &pix);
104 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 64, 64);
106 RDestroyImage(img);
107 img = RCloneImage(tile);
109 /* Draw lines in all directions to test different slopes */
110 color.red = 0xff;
111 color.green = 0x7d;
112 color.blue = 0x52;
113 color.alpha = 0xff;
114 for (i=0; i<16; i++)
115 segs[i].x1 = segs[i].y1 = 31;
117 segs[6].x2 = segs[7].x2 = segs[8].x2 = segs[9].x2 = segs[10].x2 = 0;
118 segs[2].y2 = segs[3].y2 = segs[4].y2 = segs[5].y2 = segs[6].y2 = 0;
119 segs[5].x2 = segs[11].x2 = 16;
120 segs[1].y2 = segs[7].y2 = 16;
121 segs[4].x2 = segs[12].x2 = 31;
122 segs[0].y2 = segs[8].y2 = 31;
123 segs[3].x2 = segs[13].x2 = 46;
124 segs[9].y2 = segs[15].y2 = 46;
125 segs[0].x2 = segs[1].x2 = segs[2].x2 = segs[14].x2 = segs[15].x2 = 62;
126 segs[10].y2 = segs[11].y2 = segs[12].y2 = segs[13].y2 = segs[14].y2 = 62;
127 RDrawSegments(img, segs, 9, &color);
129 /* Also test how alpha channel behaves when drawing lines */
130 color.alpha = 0x80;
131 RDrawSegments(img, &segs[9], 7, &color);
133 RConvertImage(ctx, img, &pix);
134 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 128);
136 RDestroyImage(img);
137 img = RCloneImage(tile);
139 /* Alter lines in all directions (test different slopes) */
140 cdelta.red = cdelta.green = cdelta.blue = 80;
141 cdelta.alpha = 0;
142 ROperateSegments(img, RAddOperation, segs, 16, &cdelta);
144 RConvertImage(ctx, img, &pix);
145 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 64, 128);
147 RDestroyImage(img);
149 /* Create a bevel around the icon, and save it for a later use */
150 img = tmp;
151 cdelta.red = cdelta.green = cdelta.blue = 80;
152 cdelta.alpha = 0;
153 ROperateLine(img, RAddOperation, 8, 8, 56, 8, &cdelta);
154 ROperateLine(img, RAddOperation, 8, 9, 8, 56, &cdelta);
155 cdelta.red = cdelta.green = cdelta.blue = 40;
156 cdelta.alpha = 0;
157 ROperateLine(img, RSubtractOperation, 8, 56, 56, 56, &cdelta);
158 ROperateLine(img, RSubtractOperation, 56, 8, 56, 55, &cdelta);
159 RDestroyImage(tile);
160 tmp = RCloneImage(img);
162 /* Draw some solid lines over the icon */
163 color.red = 0xff;
164 color.green = 0x7d;
165 color.blue = 0x52;
166 color.alpha = 0xff;
167 for (i=16; i<24; i++) {
168 RDrawLine(img, 9, i, 55, i, &color);
171 /* Also try some lines with alpha over the icon */
172 color.alpha = 0x80;
173 for (i=40; i<48; i++) {
174 RDrawLine(img, 9, i, 55, i, &color);
177 RConvertImage(ctx, img, &pix);
178 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 192);
180 RDestroyImage(img);
182 /* Restore the image with the icon, and alter some lines */
183 img = tmp;
184 cdelta.red = cdelta.green = cdelta.blue = 80;
185 cdelta.alpha = 0;
186 for (i=16; i<24; i++) {
187 ROperateLine(img, RSubtractOperation, 9, i, 55, i, &cdelta);
189 cdelta.red = cdelta.green = cdelta.blue = 80;
190 for (i=40; i<48; i++) {
191 ROperateLine(img, RAddOperation, 9, i, 55, i, &cdelta);
194 RConvertImage(ctx, img, &pix);
195 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 64, 192);
198 XSetWindowBackgroundPixmap(dpy, win, back);
199 XMapRaised(dpy, win);
200 XClearWindow(dpy, win);
201 XFlush(dpy);
205 void
206 testBevel()
208 RImage *img, *tile;
209 RColor color, from, to;
210 XSetWindowAttributes val;
211 Pixmap pix, back;
212 Window win;
214 val.background_pixel = ctx->black;
215 val.colormap = ctx->cmap;
216 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 140, 140,
217 0, ctx->depth, InputOutput, ctx->visual,
218 CWColormap|CWBackPixel, &val);
219 back = XCreatePixmap(ctx->dpy, ctx->drawable, 140, 140, ctx->depth);
221 /* Standard gray tile gradient */
222 from.red = 0xa6;
223 from.green = 0xa6;
224 from.blue = 0xb6;
225 from.alpha = 0xff;
226 to.red = 0x51;
227 to.green = 0x55;
228 to.blue = 0x61;
229 to.alpha = 0xff;
231 /* Dark blue tile gradient */
232 /*from.red = 0x28;
233 from.green = 0x45;
234 from.blue = 0x69;
235 from.alpha = 0xff;
236 to.red = 0x08;
237 to.green = 0x24;
238 to.blue = 0x20;
239 to.alpha = 0xff;*/
241 /* Create Background */
242 img = RCreateImage(140, 140, True);
243 color.red = 0x28;
244 color.green = 0x45;
245 color.blue = 0x69;
246 color.alpha = 0xff;
247 RClearImage(img, &color);
248 RConvertImage(ctx, img, &pix);
249 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 140, 140, 0, 0);
250 RDestroyImage(img);
252 tile = RRenderGradient(64, 64, &from, &to, RGRD_DIAGONAL);
254 img = RCloneImage(tile);
255 RBevelImage(img, RBEV_SUNKEN);
256 RConvertImage(ctx, img, &pix);
257 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 3, 3);
258 RDestroyImage(img);
260 img = RCloneImage(tile);
261 RBevelImage(img, RBEV_RAISED);
262 RConvertImage(ctx, img, &pix);
263 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 73, 3);
264 RDestroyImage(img);
266 img = RCloneImage(tile);
267 RBevelImage(img, RBEV_RAISED2);
268 RConvertImage(ctx, img, &pix);
269 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 3, 73);
270 RDestroyImage(img);
272 img = RCloneImage(tile);
273 RBevelImage(img, RBEV_RAISED3);
274 RConvertImage(ctx, img, &pix);
275 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 73, 73);
276 RDestroyImage(img);
278 XSetWindowBackgroundPixmap(dpy, win, back);
279 XMapRaised(dpy, win);
280 XClearWindow(dpy, win);
281 XFlush(dpy);
285 void
286 drawClip()
288 RImage *img;
289 RColor color, from, to, tmp;
290 RColor cdelta, cdelta1;
291 RSegment segs[20];
292 XSetWindowAttributes val;
293 Pixmap pix, back;
294 Window win;
296 val.background_pixel = ctx->black;
297 val.colormap = ctx->cmap;
298 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 64, 64,
299 0, ctx->depth, InputOutput, ctx->visual,
300 CWColormap|CWBackPixel, &val);
301 back = XCreatePixmap(ctx->dpy, ctx->drawable, 64, 64, ctx->depth);
303 /* Standard gray tile gradient */
304 from.red = 0xa6;
305 from.green = 0xa6;
306 from.blue = 0xb6;
307 from.alpha = 0xff;
308 to.red = 0x51;
309 to.green = 0x55;
310 to.blue = 0x61;
311 to.alpha = 0xff;
313 /* Dark blue tile gradient */
314 from.red = 0x28;
315 from.green = 0x45;
316 from.blue = 0x69;
317 from.alpha = 0xff;
318 to.red = 0x08;
319 to.green = 0x24;
320 to.blue = 0x20;
321 to.alpha = 0xff;
323 img = RRenderGradient(64, 64, &from, &to, RGRD_DIAGONAL);
325 RBevelImage(img, RBEV_RAISED3);
326 #if 1
327 color.alpha = 255;
328 color.red = color.green = color.blue = 0;
330 cdelta.alpha = 0;
331 cdelta.red = cdelta.green = cdelta.blue = 80;
333 cdelta1.alpha = 0;
334 cdelta1.red = cdelta1.green = cdelta1.blue = 40;
336 segs[0].x1 = segs[2].y1 = segs[4].x1 = segs[4].x2 = 63-21;
337 segs[0].x2 = segs[2].y2 = segs[1].x2 = segs[3].y2 = 63-2;
338 segs[0].y1 = segs[2].x1 = segs[1].y1 = segs[3].x1 = 2;
339 segs[0].y2 = segs[2].x2 = segs[6].x1 = segs[6].x2 = 21;
340 segs[1].x1 = segs[3].y1 = segs[5].x1 = segs[5].x2 = 63-22;
341 segs[1].y2 = segs[3].x2 = segs[7].x1 = segs[7].x2 = 22;
343 segs[4].y1 = segs[5].y1 = segs[10].x1 = segs[11].x1 = 0;
344 segs[4].y2 = segs[5].y2 = segs[10].x2 = segs[11].x2 = 1;
345 segs[6].y1 = segs[7].y1 = segs[8].x1 = segs[9].x1 = 63-1;
346 segs[6].y2 = segs[7].y2 = segs[8].x2 = segs[9].x2 = 63;
347 segs[8].y1 = segs[8].y2 = 21;
348 segs[9].y1 = segs[9].y2 = 22;
349 segs[10].y1 = segs[10].y2 = 63-21;
350 segs[11].y1 = segs[11].y2 = 63-22;
351 /* Black segments */
352 RDrawSegments(img, segs, 12, &color);
354 segs[0].x1 = segs[3].y1 = 63-20;
355 segs[0].x2 = segs[1].y2 = segs[2].x2 = segs[3].y2 = 63-2;
356 segs[0].y1 = segs[1].x1 = segs[2].y1 = segs[3].x1 = 2;
357 segs[1].y1 = segs[2].x1 = 63-23;
358 segs[0].y2 = segs[3].x2 = 20;
359 segs[1].x2 = segs[2].y2 = 23;
360 /* Bevels arround black segments */
361 ROperateSegments(img, RAddOperation, segs, 2, &cdelta);
362 ROperateSegments(img, RSubtractOperation, &segs[2], 2 ,&cdelta1);
364 RGetPixel(img, 63-2, 20, &tmp);
365 /*RPutPixel(img, 63-1, 23, &tmp);*/
366 RDrawLine(img, 63-1, 23, 63, 23, &tmp);
367 RGetPixel(img, 63-23, 2, &tmp);
368 RDrawLine(img, 63-23, 0, 63-23, 1, &tmp);
370 RGetPixel(img, 23, 63-2, &tmp);
371 /*RPutPixel(img, 23, 63-1, &tmp);*/
372 RDrawLine(img, 23, 63-1, 23, 63, &tmp);
373 RGetPixel(img, 2, 63-20, &tmp);
374 RDrawLine(img, 0, 63-23, 1, 63-23, &tmp);
375 #else
376 color.alpha = 255;
377 color.red = color.green = color.blue = 0;
379 cdelta.alpha = 0;
380 cdelta.red = cdelta.green = cdelta.blue = 80;
382 cdelta1.alpha = 0;
383 cdelta1.red = cdelta1.green = cdelta1.blue = 40;
385 RDrawLine(img, 63-21, 2, 63-2, 21, &color); /* upper 2 black lines */
386 ROperateLine(img, RAddOperation, 63-20, 2, 63-2, 20, &cdelta); /* the bevel arround them */
387 ROperateLine(img, RSubtractOperation, 63-22, 2, 63-2, 22, &cdelta1);
388 RDrawLine(img, 63-21, 0, 63-21, 1, &color); /* upper small black lines */
389 RDrawLine(img, 63-1, 21, 63, 21, &color);
391 RGetPixel(img, 63-2, 20, &tmp);
392 RPutPixel(img, 63-1, 22, &tmp);
393 RGetPixel(img, 2, 63-22, &tmp);
394 RDrawLine(img, 63-22, 0, 63-22, 1, &tmp);
396 RDrawLine(img, 2, 63-21, 21, 63-2, &color); /* lower 2 black lines */
397 ROperateLine(img, RSubtractOperation, 2, 63-20, 20, 63-2, &cdelta1); /* the bevel arround them */
398 ROperateLine(img, RAddOperation, 2, 63-22, 22, 63-2, &cdelta);
399 RDrawLine(img, 21, 63-1, 21, 63, &color); /* lower small black lines */
400 RDrawLine(img, 0, 63-21, 1, 63-21, &color);
401 ROperateLine(img, RAddOperation, 22, 63-1, 22, 63, &cdelta);
402 /*ROperateLine(img, RAddOperation, 22, 63-1, 22, 63, &cdelta);*/ /* the bevel arround them */
403 ROperateLine(img, RSubtractOperation, 0, 63-22, 1, 63-22, &cdelta1);
404 #endif
406 RConvertImage(ctx, img, &pix);
407 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 0);
408 RDestroyImage(img);
410 XSetWindowBackgroundPixmap(dpy, win, back);
411 XMapRaised(dpy, win);
412 XClearWindow(dpy, win);
413 XFlush(dpy);
418 void
419 benchmark()
421 RImage *img;
422 RColor color;
423 RColor cdelta;
424 double t1, t2, total, d1=0, d2=0, d3=0;
425 struct timeval timev;
426 int i, j;
428 puts("Starting benchmark");
430 gettimeofday(&timev, NULL);
431 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
433 img = RCreateImage(1024, 768, True);
435 gettimeofday(&timev, NULL);
436 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
437 total = t2 - t1;
438 printf("Image created in %f sec\n", total);
440 gettimeofday(&timev, NULL);
441 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
443 color.red = 0x28;
444 color.green = 0x45;
445 color.blue = 0x69;
446 color.alpha = 0xff;
447 RClearImage(img, &color);
449 color.red = 0xff;
450 color.green = 0x7d;
451 color.blue = 0x52;
452 color.alpha = 0xff;
453 cdelta.red = cdelta.green = cdelta.blue = 80;
454 cdelta.alpha = 0;
456 gettimeofday(&timev, NULL);
457 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
458 total = t2 - t1;
459 printf("Image filled in %f sec\n", total);
461 for(j=1; j<6; j++) {
462 printf("Pass %d...\n", j);
463 gettimeofday(&timev, NULL);
464 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
466 color.alpha = 0xff;
467 for (i=0; i< 10000; i++) {
468 RDrawLine(img, 0, i%64, i%64, 63, &color);
471 gettimeofday(&timev, NULL);
472 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
473 total = t2 - t1;
474 printf("Drawing 10000 lines in %f sec\n", total);
475 d1 += total;
477 gettimeofday(&timev, NULL);
478 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
480 color.alpha = 80;
481 for (i=0; i< 10000; i++) {
482 RDrawLine(img, 0, i%64, i%64, 63, &color);
485 gettimeofday(&timev, NULL);
486 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
487 total = t2 - t1;
488 printf("Drawing 10000 lines with alpha in %f sec\n", total);
489 d2+=total;
491 gettimeofday(&timev, NULL);
492 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
494 for (i=0; i< 10000; i++) {
495 ROperateLine(img, RAddOperation, 0, i%64, i%64, 63, &cdelta);
498 gettimeofday(&timev, NULL);
499 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
500 total = t2 - t1;
501 printf("Altering 10000 lines in %f sec\n", total);
502 d3+=total;
504 printf("Average: %f, %f, %f\n", d1/5, d2/5, d3/5);
506 RDestroyImage(img);
510 void main(int argc, char **argv)
512 RContextAttributes attr;
513 int visualID = -1;
515 ProgName = strrchr(argv[0],'/');
516 if (!ProgName)
517 ProgName = argv[0];
518 else
519 ProgName++;
521 dpy = XOpenDisplay("");
522 if (!dpy) {
523 puts("cant open display");
524 exit(1);
527 attr.flags = RC_RenderMode | RC_ColorsPerChannel;
529 attr.render_mode = RM_DITHER;
530 attr.colors_per_channel = 4;
532 if (visualID >= 0) {
533 attr.flags |= RC_VisualID;
534 attr.visualid = visualID;
537 ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
539 if (!ctx) {
540 printf("could not initialize graphics library context: %s\n",
541 RErrorString);
542 exit(1);
545 /* Here are the things we want to test */
546 testDraw();
548 testBevel();
550 drawClip();
552 benchmark();
554 getchar();