Initial revision
[wmaker-crm.git] / wrlib / testgrad.c
blobed7368563e38b61c05f352afe9b48b31f6c0de26
2 #include "wraster.h"
4 #include <X11/Xlib.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #ifdef BENCH
9 #include <sys/time.h>
10 #include <time.h>
11 #endif
13 Display *dpy;
14 Window win;
15 RContext *ctx;
16 RImage *imgh, *imgv, *imgd;
17 Pixmap pix;
18 char *ProgName;
21 void
22 print_help()
24 printf("usage: %s [-options] color1 [color2 ...]\n", ProgName);
25 puts("options:");
26 puts(" -m match colors");
27 puts(" -d dither colors (default)");
28 puts(" -c <cpc> colors per channel to use");
29 puts(" -v <vis-id> visual id to use");
33 void main(int argc, char **argv)
35 RContextAttributes attr;
36 RColor **colors = NULL;
37 int i, rmode = RM_DITHER, ncolors = 0, cpc = 4;
38 char **color_name;
39 XColor color;
40 XSetWindowAttributes val;
41 int visualID = -1;
42 #ifdef BENCH
43 double t1, t2, total, t, rt;
44 struct timeval timev;
45 #endif
47 ProgName = strrchr(argv[0],'/');
48 if (!ProgName)
49 ProgName = argv[0];
50 else
51 ProgName++;
53 color_name = (char **) malloc(sizeof(char*) * argc);
54 if(color_name == NULL) {
55 fprintf(stderr, "Cannot allocate memory!\n");
56 exit(1);
59 if (argc>1) {
60 for (i=1; i<argc; i++) {
61 if (strcmp(argv[i], "-m")==0) {
62 rmode = RM_MATCH;
63 } else if (strcmp(argv[i], "-d")==0) {
64 rmode = RM_DITHER;
65 } else if (strcmp(argv[i], "-c")==0) {
66 i++;
67 if (i>=argc) {
68 fprintf(stderr, "too few arguments for %s\n", argv[i-1]);
69 exit(0);
71 if (sscanf(argv[i], "%i", &cpc)!=1) {
72 fprintf(stderr, "bad value for colors per channel: \"%s\"\n", argv[i]);
73 exit(0);
75 } else if (strcmp(argv[i], "-v")==0) {
76 i++;
77 if (i>=argc) {
78 fprintf(stderr, "too few arguments for %s\n", argv[i-1]);
79 exit(0);
81 if (sscanf(argv[i], "%i", &visualID)!=1) {
82 fprintf(stderr, "bad value for visual ID: \"%s\"\n", argv[i]);
83 exit(0);
85 } else if (argv[i][0] != '-') {
86 color_name[ncolors++] = argv[i];
87 } else {
88 print_help();
89 exit(1);
94 if (ncolors == 0) {
95 print_help();
96 exit(1);
99 dpy = XOpenDisplay("");
100 if (!dpy) {
101 puts("cant open display");
102 exit(1);
105 attr.flags = RC_RenderMode | RC_ColorsPerChannel;
107 attr.render_mode = rmode;
108 attr.colors_per_channel = cpc;
110 if (visualID >= 0) {
111 attr.flags |= RC_VisualID;
112 attr.visualid = visualID;
115 ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
117 if (!ctx) {
118 printf("could not initialize graphics library context: %s\n",
119 RErrorString);
120 exit(1);
123 colors = malloc(sizeof(RColor*)*(ncolors+1));
124 for (i=0; i<ncolors; i++) {
125 if (!XParseColor(dpy, ctx->cmap, color_name[i], &color)) {
126 printf("could not parse color \"%s\"\n", color_name[i]);
127 exit(1);
129 else {
130 colors[i] = malloc(sizeof(RColor));
131 colors[i]->red = color.red >> 8;
132 colors[i]->green = color.green >> 8;
133 colors[i]->blue = color.blue >> 8;
134 printf("0x%02x%02x%02x\n", colors[i]->red, colors[i]->green,
135 colors[i]->blue);
138 colors[i] = NULL;
140 val.background_pixel = ctx->black;
141 val.colormap = ctx->cmap;
142 #ifdef BENCH
143 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 250, 250,
144 0, ctx->depth, InputOutput, ctx->visual,
145 CWColormap|CWBackPixel, &val);
146 #else
147 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 750, 250,
148 0, ctx->depth, InputOutput, ctx->visual,
149 CWColormap|CWBackPixel, &val);
150 #endif
151 XMapRaised(dpy, win);
152 XFlush(dpy);
154 #ifdef BENCH
155 rt = 0;
156 gettimeofday(&timev, NULL);
157 t = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
158 for (i=0; i<9; i++) {
159 if (i>0)
160 printf("\nrepeating...\n\n");
161 gettimeofday(&timev, NULL);
162 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
163 if (i%3==0)
164 imgh = RRenderMultiGradient(250, 250, colors, RGRD_HORIZONTAL);
165 else if (i%3==1)
166 imgh = RRenderMultiGradient(250, 250, colors, RGRD_VERTICAL);
167 else
168 imgh = RRenderMultiGradient(250, 250, colors, RGRD_DIAGONAL);
170 gettimeofday(&timev, NULL);
171 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
172 total = t2 - t1;
173 printf("gradient rendered in %f sec\n", total);
175 RConvertImage(ctx, imgh, &pix);
176 gettimeofday(&timev, NULL);
177 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
178 total = t1 - t2;
179 rt += total;
180 printf("image converted in %f sec\n", total);
182 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
184 XFlush(dpy);
186 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
187 printf("------------------------------------------\n");
188 printf("%i images processed in %f sec\n", i, t1-t);
189 printf("average time per convertion %f sec\n", rt/i);
190 printf("------------------------------------------\n");
191 #else
192 imgh = RRenderMultiGradient(250, 250, colors, RGRD_HORIZONTAL);
193 imgv = RRenderMultiGradient(250, 250, colors, RGRD_VERTICAL);
194 imgd = RRenderMultiGradient(250, 250, colors, RGRD_DIAGONAL);
196 RConvertImage(ctx, imgh, &pix);
197 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
198 RConvertImage(ctx, imgv, &pix);
199 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 250, 0);
200 RConvertImage(ctx, imgd, &pix);
201 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 500, 0);
203 XFlush(dpy);
204 #endif
206 getchar();