changed format of RImage, added x86 speicfic optimized code
[wmaker-crm.git] / wrlib / testgrad.c
blobb7b426ad22a5f960c0fa3cc342439e5ae9dfc80d
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");
34 int main(int argc, char **argv)
36 RContextAttributes attr;
37 RColor **colors = NULL;
38 int i, rmode = RDitheredRendering, ncolors = 0, cpc = 4;
39 char **color_name;
40 XColor color;
41 XSetWindowAttributes val;
42 int visualID = -1;
43 #ifdef BENCH
44 double t1, t2, total, t, rt;
45 struct timeval timev;
46 #endif
48 ProgName = strrchr(argv[0],'/');
49 if (!ProgName)
50 ProgName = argv[0];
51 else
52 ProgName++;
54 color_name = (char **) malloc(sizeof(char*) * argc);
55 if(color_name == NULL) {
56 fprintf(stderr, "Cannot allocate memory!\n");
57 exit(1);
60 if (argc>1) {
61 for (i=1; i<argc; i++) {
62 if (strcmp(argv[i], "-m")==0) {
63 rmode = RBestMatchRendering;
64 } else if (strcmp(argv[i], "-d")==0) {
65 rmode = RDitheredRendering;
66 } else if (strcmp(argv[i], "-c")==0) {
67 i++;
68 if (i>=argc) {
69 fprintf(stderr, "too few arguments for %s\n", argv[i-1]);
70 exit(0);
72 if (sscanf(argv[i], "%i", &cpc)!=1) {
73 fprintf(stderr, "bad value for colors per channel: \"%s\"\n", argv[i]);
74 exit(0);
76 } else if (strcmp(argv[i], "-v")==0) {
77 i++;
78 if (i>=argc) {
79 fprintf(stderr, "too few arguments for %s\n", argv[i-1]);
80 exit(0);
82 if (sscanf(argv[i], "%i", &visualID)!=1) {
83 fprintf(stderr, "bad value for visual ID: \"%s\"\n", argv[i]);
84 exit(0);
86 } else if (argv[i][0] != '-') {
87 color_name[ncolors++] = argv[i];
88 } else {
89 print_help();
90 exit(1);
95 if (ncolors == 0) {
96 print_help();
97 exit(1);
100 dpy = XOpenDisplay("");
101 if (!dpy) {
102 puts("cant open display");
103 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 RMessageForError(RErrorCode));
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 val.backing_store = Always;
143 #ifdef BENCH
144 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 250, 250,
145 0, ctx->depth, InputOutput, ctx->visual,
146 CWColormap|CWBackPixel|CWBackingStore, &val);
147 #else
148 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 750, 250,
149 0, ctx->depth, InputOutput, ctx->visual,
150 CWColormap|CWBackPixel|CWBackingStore, &val);
151 #endif
152 XMapRaised(dpy, win);
153 XFlush(dpy);
155 #ifdef BENCH
156 rt = 0;
157 gettimeofday(&timev, NULL);
158 t = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
159 for (i=0; i<9; i++) {
160 if (i>0)
161 printf("\nrepeating...\n\n");
162 gettimeofday(&timev, NULL);
163 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
164 if (i%3==0)
165 imgh = RRenderMultiGradient(550, 550, colors, RGRD_HORIZONTAL);
166 else if (i%3==1)
167 imgh = RRenderMultiGradient(550, 550, colors, RGRD_VERTICAL);
168 else
169 imgh = RRenderMultiGradient(550, 550, colors, RGRD_DIAGONAL);
171 gettimeofday(&timev, NULL);
172 t2 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
173 total = t2 - t1;
174 printf("gradient rendered in %f sec\n", total);
176 RConvertImage(ctx, imgh, &pix);
177 gettimeofday(&timev, NULL);
178 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
179 total = t1 - t2;
180 rt += total;
181 printf("image converted in %f sec\n", total);
183 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
185 XFlush(dpy);
187 t1 = (double)timev.tv_sec + (((double)timev.tv_usec)/1000000);
188 printf("------------------------------------------\n");
189 printf("%i images processed in %f sec\n", i, t1-t);
190 printf("average time per convertion %f sec\n", rt/i);
191 printf("------------------------------------------\n");
192 #else
193 imgh = RRenderMultiGradient(250, 250, colors, RGRD_HORIZONTAL);
194 imgv = RRenderMultiGradient(250, 250, colors, RGRD_VERTICAL);
195 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);
199 RConvertImage(ctx, imgv, &pix);
200 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 250, 0);
202 RConvertImage(ctx, imgd, &pix);
203 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 500, 0);
205 XFlush(dpy);
206 #endif
208 getchar();
209 return 0;