Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / tests / testdraw.c
blob8d416bbc3192a1ed9230e8d9f73f4a2154f1e16c
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>
11 Display *dpy;
12 RContext *ctx;
13 char *ProgName;
15 void testDraw()
17 RImage *img, *tile, *icon, *tmp;
18 RColor color, from, to;
19 RColor cdelta;
20 RSegment segs[20];
21 int i, x, y;
22 XSetWindowAttributes val;
23 Pixmap pix, back;
24 Window win;
26 val.background_pixel = ctx->black;
27 val.colormap = ctx->cmap;
28 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 128, 256,
29 0, ctx->depth, InputOutput, ctx->visual, CWColormap | CWBackPixel, &val);
30 back = XCreatePixmap(ctx->dpy, ctx->drawable, 128, 256, ctx->depth);
32 /* Dark blue tile gradient */
33 from.red = 0x28;
34 from.green = 0x45;
35 from.blue = 0x69;
36 from.alpha = 0xff;
37 to.red = 0x08;
38 to.green = 0x24;
39 to.blue = 0x20;
40 to.alpha = 0xff;
42 /* Standard gray tile gradient */
43 /*from.red = 0xa6;
44 from.green = 0xa6;
45 from.blue = 0xb6;
46 from.alpha = 0xff;
47 to.red = 0x51;
48 to.green = 0x55;
49 to.blue = 0x61;
50 to.alpha = 0xff; */
52 /* Make the tile, and put it as a sample in the first place */
53 tile = RRenderGradient(64, 64, &from, &to, RGRD_DIAGONAL);
54 img = RCloneImage(tile);
55 RConvertImage(ctx, img, &pix);
56 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 0);
58 /* Read the image, and combine it with the tile. Put it as a sample
59 * in the second slot, and also save a copy for later use. */
60 icon = RLoadImage(ctx, "ballot_box.xpm", 0);
61 if (!icon) {
62 puts(RMessageForError(RErrorCode));
63 exit(1);
65 RCombineArea(img, icon, 0, 0, icon->width, icon->height, 8, 8);
66 RReleaseImage(icon);
67 tmp = img;
68 RConvertImage(ctx, img, &pix);
69 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 64, 0);
71 img = RCloneImage(tile);
73 /* Draw random pixels on image */
74 for (i = 0; i < 200; i++) {
75 color.red = rand() % 256;
76 color.green = rand() % 256;
77 color.blue = rand() % 256;
78 color.alpha = 255;
79 x = rand() % 64;
80 y = rand() % 64;
81 RPutPixel(img, x, y, &color);
84 RConvertImage(ctx, img, &pix);
85 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 64);
87 RReleaseImage(img);
88 img = RCloneImage(tile);
90 /* Alter random pixels in image with the same amount for r/g/b */
91 for (i = 0; i < 200; i++) {
92 cdelta.red = cdelta.green = cdelta.blue = rand() % 511 - 255;
93 cdelta.alpha = 0;
94 x = rand() % 64;
95 y = rand() % 64;
96 ROperatePixel(img, RAddOperation, x, y, &cdelta);
99 RConvertImage(ctx, img, &pix);
100 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 64, 64);
102 RReleaseImage(img);
103 img = RCloneImage(tile);
105 /* Draw lines in all directions to test different slopes */
106 color.red = 0xff;
107 color.green = 0x7d;
108 color.blue = 0x52;
109 color.alpha = 0xff;
110 for (i = 0; i < 16; i++)
111 segs[i].x1 = segs[i].y1 = 31;
113 segs[6].x2 = segs[7].x2 = segs[8].x2 = segs[9].x2 = segs[10].x2 = 0;
114 segs[2].y2 = segs[3].y2 = segs[4].y2 = segs[5].y2 = segs[6].y2 = 0;
115 segs[5].x2 = segs[11].x2 = 16;
116 segs[1].y2 = segs[7].y2 = 16;
117 segs[4].x2 = segs[12].x2 = 31;
118 segs[0].y2 = segs[8].y2 = 31;
119 segs[3].x2 = segs[13].x2 = 46;
120 segs[9].y2 = segs[15].y2 = 46;
121 segs[0].x2 = segs[1].x2 = segs[2].x2 = segs[14].x2 = segs[15].x2 = 62;
122 segs[10].y2 = segs[11].y2 = segs[12].y2 = segs[13].y2 = segs[14].y2 = 62;
123 RDrawSegments(img, segs, 9, &color);
125 /* Also test how alpha channel behaves when drawing lines */
126 color.alpha = 0x80;
127 RDrawSegments(img, &segs[9], 7, &color);
129 RConvertImage(ctx, img, &pix);
130 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 128);
132 RReleaseImage(img);
133 img = RCloneImage(tile);
135 /* Alter lines in all directions (test different slopes) */
136 cdelta.red = cdelta.green = cdelta.blue = 80;
137 cdelta.alpha = 0;
138 ROperateSegments(img, RAddOperation, segs, 16, &cdelta);
140 RConvertImage(ctx, img, &pix);
141 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 64, 128);
143 RReleaseImage(img);
145 /* Create a bevel around the icon, and save it for a later use */
146 img = tmp;
147 cdelta.red = cdelta.green = cdelta.blue = 80;
148 cdelta.alpha = 0;
149 ROperateLine(img, RAddOperation, 8, 8, 56, 8, &cdelta);
150 ROperateLine(img, RAddOperation, 8, 9, 8, 56, &cdelta);
151 cdelta.red = cdelta.green = cdelta.blue = 40;
152 cdelta.alpha = 0;
153 ROperateLine(img, RSubtractOperation, 8, 56, 56, 56, &cdelta);
154 ROperateLine(img, RSubtractOperation, 56, 8, 56, 55, &cdelta);
155 RReleaseImage(tile);
156 tmp = RCloneImage(img);
158 /* Draw some solid lines over the icon */
159 color.red = 0xff;
160 color.green = 0x7d;
161 color.blue = 0x52;
162 color.alpha = 0xff;
163 for (i = 16; i < 24; i++) {
164 RDrawLine(img, 9, i, 55, i, &color);
167 /* Also try some lines with alpha over the icon */
168 color.alpha = 0x80;
169 for (i = 40; i < 48; i++) {
170 RDrawLine(img, 9, i, 55, i, &color);
173 RConvertImage(ctx, img, &pix);
174 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 192);
176 RReleaseImage(img);
178 /* Restore the image with the icon, and alter some lines */
179 img = tmp;
180 cdelta.red = cdelta.green = cdelta.blue = 80;
181 cdelta.alpha = 0;
182 for (i = 16; i < 24; i++) {
183 ROperateLine(img, RSubtractOperation, 9, i, 55, i, &cdelta);
185 cdelta.red = cdelta.green = cdelta.blue = 80;
186 for (i = 40; i < 48; i++) {
187 ROperateLine(img, RAddOperation, 9, i, 55, i, &cdelta);
190 RConvertImage(ctx, img, &pix);
191 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 64, 192);
193 XSetWindowBackgroundPixmap(dpy, win, back);
194 XMapRaised(dpy, win);
195 XClearWindow(dpy, win);
196 XFlush(dpy);
199 void testBevel()
201 RImage *img, *tile;
202 RColor color, from, to;
203 XSetWindowAttributes val;
204 Pixmap pix, back;
205 Window win;
207 val.background_pixel = ctx->black;
208 val.colormap = ctx->cmap;
209 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 140, 140,
210 0, ctx->depth, InputOutput, ctx->visual, CWColormap | CWBackPixel, &val);
211 back = XCreatePixmap(ctx->dpy, ctx->drawable, 140, 140, ctx->depth);
213 /* Standard gray tile gradient */
214 from.red = 0xa6;
215 from.green = 0xa6;
216 from.blue = 0xb6;
217 from.alpha = 0xff;
218 to.red = 0x51;
219 to.green = 0x55;
220 to.blue = 0x61;
221 to.alpha = 0xff;
223 /* Dark blue tile gradient */
224 /*from.red = 0x28;
225 from.green = 0x45;
226 from.blue = 0x69;
227 from.alpha = 0xff;
228 to.red = 0x08;
229 to.green = 0x24;
230 to.blue = 0x20;
231 to.alpha = 0xff; */
233 /* Create Background */
234 img = RCreateImage(140, 140, True);
235 color.red = 0x28;
236 color.green = 0x45;
237 color.blue = 0x69;
238 color.alpha = 0xff;
239 RClearImage(img, &color);
240 RConvertImage(ctx, img, &pix);
241 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 140, 140, 0, 0);
242 RReleaseImage(img);
244 tile = RRenderGradient(64, 64, &from, &to, RGRD_DIAGONAL);
246 img = RCloneImage(tile);
247 RBevelImage(img, RBEV_SUNKEN);
248 RConvertImage(ctx, img, &pix);
249 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 3, 3);
250 RReleaseImage(img);
252 img = RCloneImage(tile);
253 RBevelImage(img, RBEV_RAISED);
254 RConvertImage(ctx, img, &pix);
255 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 73, 3);
256 RReleaseImage(img);
258 img = RCloneImage(tile);
259 RBevelImage(img, RBEV_RAISED2);
260 RConvertImage(ctx, img, &pix);
261 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 3, 73);
262 RReleaseImage(img);
264 img = RCloneImage(tile);
265 RBevelImage(img, RBEV_RAISED3);
266 RConvertImage(ctx, img, &pix);
267 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 73, 73);
268 RReleaseImage(img);
270 XSetWindowBackgroundPixmap(dpy, win, back);
271 XMapRaised(dpy, win);
272 XClearWindow(dpy, win);
273 XFlush(dpy);
276 void testScale()
278 RImage *image;
279 RImage *scaled;
280 XSetWindowAttributes val;
281 Pixmap pix;
282 Window win;
284 val.background_pixel = ctx->black;
285 val.colormap = ctx->cmap;
286 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 140, 140,
287 0, ctx->depth, InputOutput, ctx->visual, CWColormap | CWBackPixel, &val);
288 XStoreName(dpy, win, "Scale");
289 pix = XCreatePixmap(ctx->dpy, ctx->drawable, 140, 140, ctx->depth);
291 image = RLoadImage(ctx, "ballot_box.xpm", 0);
292 if (!image) {
293 puts("couldnt load ballot_box.xpm");
294 return;
297 scaled = RScaleImage(image, 140, 140);
299 RReleaseImage(image);
300 RConvertImage(ctx, scaled, &pix);
301 XSetWindowBackgroundPixmap(dpy, win, pix);
302 XMapRaised(dpy, win);
303 XClearWindow(dpy, win);
304 XFlush(dpy);
307 void testRotate()
310 RImage *image;
311 RImage *rotated;
312 XSetWindowAttributes val;
313 Pixmap pix;
314 Window win;
316 image = RLoadImage(ctx, "ballot_box.xpm", 0);
317 if (!image) {
318 puts("couldnt load ballot_box.xpm");
319 return;
322 image = RScaleImage(image, 90, 180);
324 val.background_pixel = ctx->black;
325 val.colormap = ctx->cmap;
326 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, image->height,
327 image->width, 0, ctx->depth, InputOutput, ctx->visual, CWColormap | CWBackPixel, &val);
328 XStoreName(dpy, win, "Rotate");
329 pix = XCreatePixmap(ctx->dpy, ctx->drawable, image->height, image->width, ctx->depth);
331 rotated = RRotateImage(image, 90.0);
333 RReleaseImage(image);
334 RConvertImage(ctx, rotated, &pix);
335 XSetWindowBackgroundPixmap(dpy, win, pix);
336 XMapRaised(dpy, win);
337 XClearWindow(dpy, win);
338 XFlush(dpy);
341 void drawClip()
343 RImage *img;
344 RColor color, from, to, tmp;
345 RColor cdelta, cdelta1;
346 RSegment segs[20];
347 XSetWindowAttributes val;
348 Pixmap pix, back;
349 Window win;
351 val.background_pixel = ctx->black;
352 val.colormap = ctx->cmap;
353 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 64, 64,
354 0, ctx->depth, InputOutput, ctx->visual, CWColormap | CWBackPixel, &val);
355 back = XCreatePixmap(ctx->dpy, ctx->drawable, 64, 64, ctx->depth);
357 /* Standard gray tile gradient */
358 from.red = 0xa6;
359 from.green = 0xa6;
360 from.blue = 0xb6;
361 from.alpha = 0xff;
362 to.red = 0x51;
363 to.green = 0x55;
364 to.blue = 0x61;
365 to.alpha = 0xff;
367 /* Dark blue tile gradient */
368 from.red = 0x28;
369 from.green = 0x45;
370 from.blue = 0x69;
371 from.alpha = 0xff;
372 to.red = 0x08;
373 to.green = 0x24;
374 to.blue = 0x20;
375 to.alpha = 0xff;
377 img = RRenderGradient(64, 64, &from, &to, RGRD_DIAGONAL);
379 RBevelImage(img, RBEV_RAISED3);
380 #if 1
381 color.alpha = 255;
382 color.red = color.green = color.blue = 0;
384 cdelta.alpha = 0;
385 cdelta.red = cdelta.green = cdelta.blue = 80;
387 cdelta1.alpha = 0;
388 cdelta1.red = cdelta1.green = cdelta1.blue = 40;
390 segs[0].x1 = segs[2].y1 = segs[4].x1 = segs[4].x2 = 63 - 21;
391 segs[0].x2 = segs[2].y2 = segs[1].x2 = segs[3].y2 = 63 - 2;
392 segs[0].y1 = segs[2].x1 = segs[1].y1 = segs[3].x1 = 2;
393 segs[0].y2 = segs[2].x2 = segs[6].x1 = segs[6].x2 = 21;
394 segs[1].x1 = segs[3].y1 = segs[5].x1 = segs[5].x2 = 63 - 22;
395 segs[1].y2 = segs[3].x2 = segs[7].x1 = segs[7].x2 = 22;
397 segs[4].y1 = segs[5].y1 = segs[10].x1 = segs[11].x1 = 0;
398 segs[4].y2 = segs[5].y2 = segs[10].x2 = segs[11].x2 = 1;
399 segs[6].y1 = segs[7].y1 = segs[8].x1 = segs[9].x1 = 63 - 1;
400 segs[6].y2 = segs[7].y2 = segs[8].x2 = segs[9].x2 = 63;
401 segs[8].y1 = segs[8].y2 = 21;
402 segs[9].y1 = segs[9].y2 = 22;
403 segs[10].y1 = segs[10].y2 = 63 - 21;
404 segs[11].y1 = segs[11].y2 = 63 - 22;
405 /* Black segments */
406 RDrawSegments(img, segs, 12, &color);
408 segs[0].x1 = segs[3].y1 = 63 - 20;
409 segs[0].x2 = segs[1].y2 = segs[2].x2 = segs[3].y2 = 63 - 2;
410 segs[0].y1 = segs[1].x1 = segs[2].y1 = segs[3].x1 = 2;
411 segs[1].y1 = segs[2].x1 = 63 - 23;
412 segs[0].y2 = segs[3].x2 = 20;
413 segs[1].x2 = segs[2].y2 = 23;
414 /* Bevels arround black segments */
415 ROperateSegments(img, RAddOperation, segs, 2, &cdelta);
416 ROperateSegments(img, RSubtractOperation, &segs[2], 2, &cdelta1);
418 RGetPixel(img, 63 - 2, 20, &tmp);
419 /*RPutPixel(img, 63-1, 23, &tmp); */
420 RDrawLine(img, 63 - 1, 23, 63, 23, &tmp);
421 RGetPixel(img, 63 - 23, 2, &tmp);
422 RDrawLine(img, 63 - 23, 0, 63 - 23, 1, &tmp);
424 RGetPixel(img, 23, 63 - 2, &tmp);
425 /*RPutPixel(img, 23, 63-1, &tmp); */
426 RDrawLine(img, 23, 63 - 1, 23, 63, &tmp);
427 RGetPixel(img, 2, 63 - 20, &tmp);
428 RDrawLine(img, 0, 63 - 23, 1, 63 - 23, &tmp);
429 #else
430 color.alpha = 255;
431 color.red = color.green = color.blue = 0;
433 cdelta.alpha = 0;
434 cdelta.red = cdelta.green = cdelta.blue = 80;
436 cdelta1.alpha = 0;
437 cdelta1.red = cdelta1.green = cdelta1.blue = 40;
439 RDrawLine(img, 63 - 21, 2, 63 - 2, 21, &color); /* upper 2 black lines */
440 ROperateLine(img, RAddOperation, 63 - 20, 2, 63 - 2, 20, &cdelta); /* the bevel arround them */
441 ROperateLine(img, RSubtractOperation, 63 - 22, 2, 63 - 2, 22, &cdelta1);
442 RDrawLine(img, 63 - 21, 0, 63 - 21, 1, &color); /* upper small black lines */
443 RDrawLine(img, 63 - 1, 21, 63, 21, &color);
445 RGetPixel(img, 63 - 2, 20, &tmp);
446 RPutPixel(img, 63 - 1, 22, &tmp);
447 RGetPixel(img, 2, 63 - 22, &tmp);
448 RDrawLine(img, 63 - 22, 0, 63 - 22, 1, &tmp);
450 RDrawLine(img, 2, 63 - 21, 21, 63 - 2, &color); /* lower 2 black lines */
451 ROperateLine(img, RSubtractOperation, 2, 63 - 20, 20, 63 - 2, &cdelta1); /* the bevel arround them */
452 ROperateLine(img, RAddOperation, 2, 63 - 22, 22, 63 - 2, &cdelta);
453 RDrawLine(img, 21, 63 - 1, 21, 63, &color); /* lower small black lines */
454 RDrawLine(img, 0, 63 - 21, 1, 63 - 21, &color);
455 ROperateLine(img, RAddOperation, 22, 63 - 1, 22, 63, &cdelta);
456 /*ROperateLine(img, RAddOperation, 22, 63-1, 22, 63, &cdelta); *//* the bevel arround them */
457 ROperateLine(img, RSubtractOperation, 0, 63 - 22, 1, 63 - 22, &cdelta1);
458 #endif
460 RConvertImage(ctx, img, &pix);
461 XCopyArea(dpy, pix, back, ctx->copy_gc, 0, 0, 64, 64, 0, 0);
462 RReleaseImage(img);
464 XSetWindowBackgroundPixmap(dpy, win, back);
465 XMapRaised(dpy, win);
466 XClearWindow(dpy, win);
467 XFlush(dpy);
470 void benchmark()
472 RImage *img;
473 RColor color;
474 RColor cdelta;
475 double t1, t2, total, d1 = 0, d2 = 0, d3 = 0;
476 struct timeval timev;
477 int i, j;
479 puts("Starting benchmark");
481 gettimeofday(&timev, NULL);
482 t1 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
484 img = RCreateImage(1024, 768, True);
486 gettimeofday(&timev, NULL);
487 t2 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
488 total = t2 - t1;
489 printf("Image created in %f sec\n", total);
491 gettimeofday(&timev, NULL);
492 t1 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
494 color.red = 0x28;
495 color.green = 0x45;
496 color.blue = 0x69;
497 color.alpha = 0xff;
498 RClearImage(img, &color);
500 color.red = 0xff;
501 color.green = 0x7d;
502 color.blue = 0x52;
503 color.alpha = 0xff;
504 cdelta.red = cdelta.green = cdelta.blue = 80;
505 cdelta.alpha = 0;
507 gettimeofday(&timev, NULL);
508 t2 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
509 total = t2 - t1;
510 printf("Image filled in %f sec\n", total);
512 for (j = 1; j < 6; j++) {
513 printf("Pass %d...\n", j);
514 gettimeofday(&timev, NULL);
515 t1 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
517 color.alpha = 0xff;
518 for (i = 0; i < 10000; i++) {
519 RDrawLine(img, 0, i % 64, i % 64, 63, &color);
522 gettimeofday(&timev, NULL);
523 t2 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
524 total = t2 - t1;
525 printf("Drawing 10000 lines in %f sec\n", total);
526 d1 += total;
528 gettimeofday(&timev, NULL);
529 t1 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
531 color.alpha = 80;
532 for (i = 0; i < 10000; i++) {
533 RDrawLine(img, 0, i % 64, i % 64, 63, &color);
536 gettimeofday(&timev, NULL);
537 t2 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
538 total = t2 - t1;
539 printf("Drawing 10000 lines with alpha in %f sec\n", total);
540 d2 += total;
542 gettimeofday(&timev, NULL);
543 t1 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
545 for (i = 0; i < 10000; i++) {
546 ROperateLine(img, RAddOperation, 0, i % 64, i % 64, 63, &cdelta);
549 gettimeofday(&timev, NULL);
550 t2 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
551 total = t2 - t1;
552 printf("Altering 10000 lines in %f sec\n", total);
553 d3 += total;
555 printf("Average: %f, %f, %f\n", d1 / 5, d2 / 5, d3 / 5);
557 RReleaseImage(img);
560 int main(int argc, char **argv)
562 RContextAttributes attr;
563 int visualID = -1;
565 ProgName = strrchr(argv[0], '/');
566 if (!ProgName)
567 ProgName = argv[0];
568 else
569 ProgName++;
571 dpy = XOpenDisplay("");
572 if (!dpy) {
573 puts("cant open display");
574 exit(1);
577 attr.flags = RC_RenderMode | RC_ColorsPerChannel;
579 attr.render_mode = RDitheredRendering;
580 attr.colors_per_channel = 4;
582 if (visualID >= 0) {
583 attr.flags |= RC_VisualID;
584 attr.visualid = visualID;
587 ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
589 if (!ctx) {
590 printf("could not initialize graphics library context: %s\n", RMessageForError(RErrorCode));
591 exit(1);
594 /* Here are the things we want to test */
595 testDraw();
597 testBevel();
599 drawClip();
601 testScale();
603 testRotate();
605 /* benchmark(); */
607 getchar();
608 return 0;