changed indentation to use spaces only
[wmaker-crm.git] / wrlib / testdraw.c
blobae7a4ea53d74b20a677523112d0411931d274aff
2 #include <X11/Xlib.h>
3 #include "wraster.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include <sys/time.h>
9 #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(RMessageForError(RErrorCode));
67 exit(1);
69 RCombineArea(img, icon, 0, 0, icon->width, icon->height, 8, 8);
70 RReleaseImage(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 RReleaseImage(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 RReleaseImage(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 RReleaseImage(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 RReleaseImage(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 RReleaseImage(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 RReleaseImage(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 RReleaseImage(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 RReleaseImage(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 RReleaseImage(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 RReleaseImage(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 RReleaseImage(img);
278 XSetWindowBackgroundPixmap(dpy, win, back);
279 XMapRaised(dpy, win);
280 XClearWindow(dpy, win);
281 XFlush(dpy);
285 void testScale()
287 RImage *image;
288 RImage *scaled;
289 XSetWindowAttributes val;
290 Pixmap pix;
291 Window win;
293 val.background_pixel = ctx->black;
294 val.colormap = ctx->cmap;
295 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 140, 140,
296 0, ctx->depth, InputOutput, ctx->visual,
297 CWColormap|CWBackPixel, &val);
298 XStoreName(dpy, win, "Scale");
299 pix = XCreatePixmap(ctx->dpy, ctx->drawable, 140, 140, ctx->depth);
301 image = RLoadImage(ctx, "ballot_box.xpm", 0);
302 if (!image) {
303 puts("couldnt load ballot_box.xpm");
304 return;
307 scaled = RScaleImage(image, 140, 140);
309 RReleaseImage(image);
310 RConvertImage(ctx, scaled, &pix);
311 XSetWindowBackgroundPixmap(dpy, win, pix);
312 XMapRaised(dpy, win);
313 XClearWindow(dpy, win);
314 XFlush(dpy);
318 void testRotate()
321 RImage *image;
322 RImage *rotated;
323 XSetWindowAttributes val;
324 Pixmap pix;
325 Window win;
327 image = RLoadImage(ctx, "ballot_box.xpm", 0);
328 if (!image) {
329 puts("couldnt load ballot_box.xpm");
330 return;
333 image = RScaleImage(image, 90, 180);
336 val.background_pixel = ctx->black;
337 val.colormap = ctx->cmap;
338 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, image->height,
339 image->width,
340 0, ctx->depth, InputOutput, ctx->visual,
341 CWColormap|CWBackPixel, &val);
342 XStoreName(dpy, win, "Rotate");
343 pix = XCreatePixmap(ctx->dpy, ctx->drawable, image->height, image->width,
344 ctx->depth);
346 rotated = RRotateImage(image, 90.0);
348 RReleaseImage(image);
349 RConvertImage(ctx, rotated, &pix);
350 XSetWindowBackgroundPixmap(dpy, win, pix);
351 XMapRaised(dpy, win);
352 XClearWindow(dpy, win);
353 XFlush(dpy);
358 void
359 drawClip()
361 RImage *img;
362 RColor color, from, to, tmp;
363 RColor cdelta, cdelta1;
364 RSegment segs[20];
365 XSetWindowAttributes val;
366 Pixmap pix, back;
367 Window win;
369 val.background_pixel = ctx->black;
370 val.colormap = ctx->cmap;
371 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 64, 64,
372 0, ctx->depth, InputOutput, ctx->visual,
373 CWColormap|CWBackPixel, &val);
374 back = XCreatePixmap(ctx->dpy, ctx->drawable, 64, 64, ctx->depth);
376 /* Standard gray tile gradient */
377 from.red = 0xa6;
378 from.green = 0xa6;
379 from.blue = 0xb6;
380 from.alpha = 0xff;
381 to.red = 0x51;
382 to.green = 0x55;
383 to.blue = 0x61;
384 to.alpha = 0xff;
386 /* Dark blue tile gradient */
387 from.red = 0x28;
388 from.green = 0x45;
389 from.blue = 0x69;
390 from.alpha = 0xff;
391 to.red = 0x08;
392 to.green = 0x24;
393 to.blue = 0x20;
394 to.alpha = 0xff;
396 img = RRenderGradient(64, 64, &from, &to, RGRD_DIAGONAL);
398 RBevelImage(img, RBEV_RAISED3);
399 #if 1
400 color.alpha = 255;
401 color.red = color.green = color.blue = 0;
403 cdelta.alpha = 0;
404 cdelta.red = cdelta.green = cdelta.blue = 80;
406 cdelta1.alpha = 0;
407 cdelta1.red = cdelta1.green = cdelta1.blue = 40;
409 segs[0].x1 = segs[2].y1 = segs[4].x1 = segs[4].x2 = 63-21;
410 segs[0].x2 = segs[2].y2 = segs[1].x2 = segs[3].y2 = 63-2;
411 segs[0].y1 = segs[2].x1 = segs[1].y1 = segs[3].x1 = 2;
412 segs[0].y2 = segs[2].x2 = segs[6].x1 = segs[6].x2 = 21;
413 segs[1].x1 = segs[3].y1 = segs[5].x1 = segs[5].x2 = 63-22;
414 segs[1].y2 = segs[3].x2 = segs[7].x1 = segs[7].x2 = 22;
416 segs[4].y1 = segs[5].y1 = segs[10].x1 = segs[11].x1 = 0;
417 segs[4].y2 = segs[5].y2 = segs[10].x2 = segs[11].x2 = 1;
418 segs[6].y1 = segs[7].y1 = segs[8].x1 = segs[9].x1 = 63-1;
419 segs[6].y2 = segs[7].y2 = segs[8].x2 = segs[9].x2 = 63;
420 segs[8].y1 = segs[8].y2 = 21;
421 segs[9].y1 = segs[9].y2 = 22;
422 segs[10].y1 = segs[10].y2 = 63-21;
423 segs[11].y1 = segs[11].y2 = 63-22;
424 /* Black segments */
425 RDrawSegments(img, segs, 12, &color);
427 segs[0].x1 = segs[3].y1 = 63-20;
428 segs[0].x2 = segs[1].y2 = segs[2].x2 = segs[3].y2 = 63-2;
429 segs[0].y1 = segs[1].x1 = segs[2].y1 = segs[3].x1 = 2;
430 segs[1].y1 = segs[2].x1 = 63-23;
431 segs[0].y2 = segs[3].x2 = 20;
432 segs[1].x2 = segs[2].y2 = 23;
433 /* Bevels arround black segments */
434 ROperateSegments(img, RAddOperation, segs, 2, &cdelta);
435 ROperateSegments(img, RSubtractOperation, &segs[2], 2 ,&cdelta1);
437 RGetPixel(img, 63-2, 20, &tmp);
438 /*RPutPixel(img, 63-1, 23, &tmp);*/
439 RDrawLine(img, 63-1, 23, 63, 23, &tmp);
440 RGetPixel(img, 63-23, 2, &tmp);
441 RDrawLine(img, 63-23, 0, 63-23, 1, &tmp);
443 RGetPixel(img, 23, 63-2, &tmp);
444 /*RPutPixel(img, 23, 63-1, &tmp);*/
445 RDrawLine(img, 23, 63-1, 23, 63, &tmp);
446 RGetPixel(img, 2, 63-20, &tmp);
447 RDrawLine(img, 0, 63-23, 1, 63-23, &tmp);
448 #else
449 color.alpha = 255;
450 color.red = color.green = color.blue = 0;
452 cdelta.alpha = 0;
453 cdelta.red = cdelta.green = cdelta.blue = 80;
455 cdelta1.alpha = 0;
456 cdelta1.red = cdelta1.green = cdelta1.blue = 40;
458 RDrawLine(img, 63-21, 2, 63-2, 21, &color); /* upper 2 black lines */
459 ROperateLine(img, RAddOperation, 63-20, 2, 63-2, 20, &cdelta); /* the bevel arround them */
460 ROperateLine(img, RSubtractOperation, 63-22, 2, 63-2, 22, &cdelta1);
461 RDrawLine(img, 63-21, 0, 63-21, 1, &color); /* upper small black lines */
462 RDrawLine(img, 63-1, 21, 63, 21, &color);
464 RGetPixel(img, 63-2, 20, &tmp);
465 RPutPixel(img, 63-1, 22, &tmp);
466 RGetPixel(img, 2, 63-22, &tmp);
467 RDrawLine(img, 63-22, 0, 63-22, 1, &tmp);
469 RDrawLine(img, 2, 63-21, 21, 63-2, &color); /* lower 2 black lines */
470 ROperateLine(img, RSubtractOperation, 2, 63-20, 20, 63-2, &cdelta1); /* the bevel arround them */
471 ROperateLine(img, RAddOperation, 2, 63-22, 22, 63-2, &cdelta);
472 RDrawLine(img, 21, 63-1, 21, 63, &color); /* lower small black lines */
473 RDrawLine(img, 0, 63-21, 1, 63-21, &color);
474 ROperateLine(img, RAddOperation, 22, 63-1, 22, 63, &cdelta);
475 /*ROperateLine(img, RAddOperation, 22, 63-1, 22, 63, &cdelta);*/ /* the bevel arround them */
476 ROperateLine(img, RSubtractOperation, 0, 63-22, 1, 63-22, &cdelta1);
477 #endif
479 RConvertImage(ctx, img, &pix);
480 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 0);
481 RReleaseImage(img);
483 XSetWindowBackgroundPixmap(dpy, win, back);
484 XMapRaised(dpy, win);
485 XClearWindow(dpy, win);
486 XFlush(dpy);
491 void
492 benchmark()
494 RImage *img;
495 RColor color;
496 RColor cdelta;
497 double t1, t2, total, d1=0, d2=0, d3=0;
498 struct timeval timev;
499 int i, j;
501 puts("Starting benchmark");
503 gettimeofday(&timev, NULL);
504 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
506 img = RCreateImage(1024, 768, True);
508 gettimeofday(&timev, NULL);
509 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
510 total = t2 - t1;
511 printf("Image created in %f sec\n", total);
513 gettimeofday(&timev, NULL);
514 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
516 color.red = 0x28;
517 color.green = 0x45;
518 color.blue = 0x69;
519 color.alpha = 0xff;
520 RClearImage(img, &color);
522 color.red = 0xff;
523 color.green = 0x7d;
524 color.blue = 0x52;
525 color.alpha = 0xff;
526 cdelta.red = cdelta.green = cdelta.blue = 80;
527 cdelta.alpha = 0;
529 gettimeofday(&timev, NULL);
530 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
531 total = t2 - t1;
532 printf("Image filled in %f sec\n", total);
534 for(j=1; j<6; j++) {
535 printf("Pass %d...\n", j);
536 gettimeofday(&timev, NULL);
537 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
539 color.alpha = 0xff;
540 for (i=0; i< 10000; i++) {
541 RDrawLine(img, 0, i%64, i%64, 63, &color);
544 gettimeofday(&timev, NULL);
545 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
546 total = t2 - t1;
547 printf("Drawing 10000 lines in %f sec\n", total);
548 d1 += total;
550 gettimeofday(&timev, NULL);
551 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
553 color.alpha = 80;
554 for (i=0; i< 10000; i++) {
555 RDrawLine(img, 0, i%64, i%64, 63, &color);
558 gettimeofday(&timev, NULL);
559 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
560 total = t2 - t1;
561 printf("Drawing 10000 lines with alpha in %f sec\n", total);
562 d2+=total;
564 gettimeofday(&timev, NULL);
565 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
567 for (i=0; i< 10000; i++) {
568 ROperateLine(img, RAddOperation, 0, i%64, i%64, 63, &cdelta);
571 gettimeofday(&timev, NULL);
572 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
573 total = t2 - t1;
574 printf("Altering 10000 lines in %f sec\n", total);
575 d3+=total;
577 printf("Average: %f, %f, %f\n", d1/5, d2/5, d3/5);
579 RReleaseImage(img);
583 int main(int argc, char **argv)
585 RContextAttributes attr;
586 int visualID = -1;
588 ProgName = strrchr(argv[0],'/');
589 if (!ProgName)
590 ProgName = argv[0];
591 else
592 ProgName++;
594 dpy = XOpenDisplay("");
595 if (!dpy) {
596 puts("cant open display");
597 exit(1);
600 attr.flags = RC_RenderMode | RC_ColorsPerChannel;
602 attr.render_mode = RDitheredRendering;
603 attr.colors_per_channel = 4;
605 if (visualID >= 0) {
606 attr.flags |= RC_VisualID;
607 attr.visualid = visualID;
610 ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
612 if (!ctx) {
613 printf("could not initialize graphics library context: %s\n",
614 RMessageForError(RErrorCode));
615 exit(1);
618 /* Here are the things we want to test */
619 testDraw();
621 testBevel();
623 drawClip();
625 testScale();
627 testRotate();
629 /* benchmark();*/
631 getchar();
632 return 0;