changed indentation to use spaces only
[wmaker-crm.git] / wrlib / testgrad.c
blob42c2193ebd4512875e6e5d248a7c483edae72f7f
3 #include <X11/Xlib.h>
4 #include "wraster.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
14 Display *dpy;
15 Window win;
16 RContext *ctx;
17 RImage *imgh, *imgv, *imgd;
18 Pixmap pix;
19 char *ProgName;
22 void
23 print_help()
25 printf("usage: %s [-options] color1 [color2 ...]\n", ProgName);
26 puts("options:");
27 puts(" -m match colors");
28 puts(" -d dither colors (default)");
29 puts(" -c <cpc> colors per channel to use");
30 puts(" -v <vis-id> visual id to use");
33 #ifdef BENCH
34 #include "bench.h"
35 #endif
36 int main(int argc, char **argv)
38 RContextAttributes attr;
39 RColor **colors = NULL;
40 int i, rmode = RDitheredRendering, ncolors = 0, cpc = 4;
41 char **color_name;
42 XColor color;
43 XSetWindowAttributes val;
44 int visualID = -1;
45 #ifdef BENCH
46 double t1, t2, total, t, rt;
47 struct timeval timev;
48 #endif
50 ProgName = strrchr(argv[0],'/');
51 if (!ProgName)
52 ProgName = argv[0];
53 else
54 ProgName++;
56 color_name = (char **) malloc(sizeof(char*) * argc);
57 if(color_name == NULL) {
58 fprintf(stderr, "Cannot allocate memory!\n");
59 exit(1);
62 if (argc>1) {
63 for (i=1; i<argc; i++) {
64 if (strcmp(argv[i], "-m")==0) {
65 rmode = RBestMatchRendering;
66 } else if (strcmp(argv[i], "-d")==0) {
67 rmode = RDitheredRendering;
68 } else if (strcmp(argv[i], "-c")==0) {
69 i++;
70 if (i>=argc) {
71 fprintf(stderr, "too few arguments for %s\n", argv[i-1]);
72 exit(0);
74 if (sscanf(argv[i], "%i", &cpc)!=1) {
75 fprintf(stderr, "bad value for colors per channel: \"%s\"\n", argv[i]);
76 exit(0);
78 } else if (strcmp(argv[i], "-v")==0) {
79 i++;
80 if (i>=argc) {
81 fprintf(stderr, "too few arguments for %s\n", argv[i-1]);
82 exit(0);
84 if (sscanf(argv[i], "%i", &visualID)!=1) {
85 fprintf(stderr, "bad value for visual ID: \"%s\"\n", argv[i]);
86 exit(0);
88 } else if (argv[i][0] != '-') {
89 color_name[ncolors++] = argv[i];
90 } else {
91 print_help();
92 exit(1);
97 if (ncolors == 0) {
98 print_help();
99 exit(1);
102 dpy = XOpenDisplay("");
103 if (!dpy) {
104 puts("cant open display");
105 exit(1);
107 attr.flags = RC_RenderMode | RC_ColorsPerChannel;
109 attr.render_mode = rmode;
110 attr.colors_per_channel = cpc;
112 if (visualID >= 0) {
113 attr.flags |= RC_VisualID;
114 attr.visualid = visualID;
117 ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
119 if (!ctx) {
120 printf("could not initialize graphics library context: %s\n",
121 RMessageForError(RErrorCode));
122 exit(1);
125 colors = malloc(sizeof(RColor*)*(ncolors+1));
126 for (i=0; i<ncolors; i++) {
127 if (!XParseColor(dpy, ctx->cmap, color_name[i], &color)) {
128 printf("could not parse color \"%s\"\n", color_name[i]);
129 exit(1);
131 else {
132 colors[i] = malloc(sizeof(RColor));
133 colors[i]->red = color.red >> 8;
134 colors[i]->green = color.green >> 8;
135 colors[i]->blue = color.blue >> 8;
136 printf("0x%02x%02x%02x\n", colors[i]->red, colors[i]->green,
137 colors[i]->blue);
140 colors[i] = NULL;
142 val.background_pixel = ctx->black;
143 val.colormap = ctx->cmap;
144 val.backing_store = Always;
145 #ifdef BENCH
146 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 250, 250,
147 0, ctx->depth, InputOutput, ctx->visual,
148 CWColormap|CWBackPixel|CWBackingStore, &val);
149 #else
150 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 750, 250,
151 0, ctx->depth, InputOutput, ctx->visual,
152 CWColormap|CWBackPixel|CWBackingStore, &val);
153 #endif
154 XMapRaised(dpy, win);
155 XFlush(dpy);
157 #ifdef BENCH
158 rt = 0;
159 gettimeofday(&timev, NULL);
160 t = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
161 for (i=0; i<9; i++) {
162 if (i>0)
163 printf("\nrepeating...\n\n");
164 gettimeofday(&timev, NULL);
165 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
166 if (i%3==0)
167 imgh = RRenderMultiGradient(550, 550, colors, RGRD_HORIZONTAL);
168 else if (i%3==1)
169 imgh = RRenderMultiGradient(550, 550, colors, RGRD_VERTICAL);
170 else
171 imgh = RRenderMultiGradient(550, 550, colors, RGRD_DIAGONAL);
173 gettimeofday(&timev, NULL);
174 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
175 total = t2 - t1;
176 printf("gradient rendered in %f sec\n", total);
178 RConvertImage(ctx, imgh, &pix);
179 gettimeofday(&timev, NULL);
180 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
181 total = t1 - t2;
182 rt += total;
183 printf("image converted in %f sec\n", total);
185 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
187 XFlush(dpy);
189 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
190 printf("------------------------------------------\n");
191 printf("%i images processed in %f sec\n", i, t1-t);
192 printf("average time per convertion %f sec\n", rt/i);
193 printf("------------------------------------------\n");
194 #else
195 imgh = RRenderMultiGradient(250, 250, colors, RGRD_HORIZONTAL);
196 imgv = RRenderMultiGradient(250, 250, colors, RGRD_VERTICAL);
197 imgd = RRenderMultiGradient(250, 250, colors, RGRD_DIAGONAL);
198 RConvertImage(ctx, imgh, &pix);
199 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
201 RConvertImage(ctx, imgv, &pix);
202 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 250, 0);
204 RConvertImage(ctx, imgd, &pix);
205 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 500, 0);
207 XFlush(dpy);
208 #endif
210 getchar();
211 return 0;