Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / tests / testgrad.c
blob1f43f2cd96f9db55c0b89d1d6459c4883b558d4a
2 #include <X11/Xlib.h>
3 #include "wraster.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #ifdef BENCH
8 #include <sys/time.h>
9 #include <time.h>
10 #endif
12 Display *dpy;
13 Window win;
14 RContext *ctx;
15 RImage *imgh, *imgv, *imgd;
16 Pixmap pix;
17 char *ProgName;
19 void print_help()
21 printf("usage: %s [-options] color1 [color2 ...]\n", ProgName);
22 puts("options:");
23 puts(" -m match colors");
24 puts(" -d dither colors (default)");
25 puts(" -c <cpc> colors per channel to use");
26 puts(" -v <vis-id> visual id to use");
29 #ifdef BENCH
30 #include "bench.h"
31 #endif
32 int main(int argc, char **argv)
34 RContextAttributes attr;
35 RColor **colors = NULL;
36 int i, rmode = RDitheredRendering, ncolors = 0, cpc = 4;
37 char **color_name;
38 XColor color;
39 XSetWindowAttributes val;
40 int visualID = -1;
41 #ifdef BENCH
42 double t1, t2, total, t, rt;
43 struct timeval timev;
44 #endif
46 ProgName = strrchr(argv[0], '/');
47 if (!ProgName)
48 ProgName = argv[0];
49 else
50 ProgName++;
52 color_name = (char **)malloc(sizeof(char *) * argc);
53 if (color_name == NULL) {
54 fprintf(stderr, "Cannot allocate memory!\n");
55 exit(1);
58 if (argc > 1) {
59 for (i = 1; i < argc; i++) {
60 if (strcmp(argv[i], "-m") == 0) {
61 rmode = RBestMatchRendering;
62 } else if (strcmp(argv[i], "-d") == 0) {
63 rmode = RDitheredRendering;
64 } else if (strcmp(argv[i], "-c") == 0) {
65 i++;
66 if (i >= argc) {
67 fprintf(stderr, "too few arguments for %s\n", argv[i - 1]);
68 exit(0);
70 if (sscanf(argv[i], "%i", &cpc) != 1) {
71 fprintf(stderr, "bad value for colors per channel: \"%s\"\n", argv[i]);
72 exit(0);
74 } else if (strcmp(argv[i], "-v") == 0) {
75 i++;
76 if (i >= argc) {
77 fprintf(stderr, "too few arguments for %s\n", argv[i - 1]);
78 exit(0);
80 if (sscanf(argv[i], "%i", &visualID) != 1) {
81 fprintf(stderr, "bad value for visual ID: \"%s\"\n", argv[i]);
82 exit(0);
84 } else if (argv[i][0] != '-') {
85 color_name[ncolors++] = argv[i];
86 } else {
87 print_help();
88 exit(1);
93 if (ncolors == 0) {
94 print_help();
95 exit(1);
98 dpy = XOpenDisplay("");
99 if (!dpy) {
100 puts("cant open display");
101 exit(1);
103 attr.flags = RC_RenderMode | RC_ColorsPerChannel;
105 attr.render_mode = rmode;
106 attr.colors_per_channel = cpc;
108 if (visualID >= 0) {
109 attr.flags |= RC_VisualID;
110 attr.visualid = visualID;
113 ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
115 if (!ctx) {
116 printf("could not initialize graphics library context: %s\n", RMessageForError(RErrorCode));
117 exit(1);
120 colors = malloc(sizeof(RColor *) * (ncolors + 1));
121 for (i = 0; i < ncolors; i++) {
122 if (!XParseColor(dpy, ctx->cmap, color_name[i], &color)) {
123 printf("could not parse color \"%s\"\n", color_name[i]);
124 exit(1);
125 } else {
126 colors[i] = malloc(sizeof(RColor));
127 colors[i]->red = color.red >> 8;
128 colors[i]->green = color.green >> 8;
129 colors[i]->blue = color.blue >> 8;
130 printf("0x%02x%02x%02x\n", colors[i]->red, colors[i]->green, colors[i]->blue);
133 colors[i] = NULL;
135 val.background_pixel = ctx->black;
136 val.colormap = ctx->cmap;
137 val.backing_store = Always;
138 #ifdef BENCH
139 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 250, 250,
140 0, ctx->depth, InputOutput, ctx->visual,
141 CWColormap | CWBackPixel | CWBackingStore, &val);
142 #else
143 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 750, 250,
144 0, ctx->depth, InputOutput, ctx->visual,
145 CWColormap | CWBackPixel | CWBackingStore, &val);
146 #endif
147 XMapRaised(dpy, win);
148 XFlush(dpy);
150 #ifdef BENCH
151 rt = 0;
152 gettimeofday(&timev, NULL);
153 t = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
154 for (i = 0; i < 9; i++) {
155 if (i > 0)
156 printf("\nrepeating...\n\n");
157 gettimeofday(&timev, NULL);
158 t1 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
159 if (i % 3 == 0)
160 imgh = RRenderMultiGradient(550, 550, colors, RGRD_HORIZONTAL);
161 else if (i % 3 == 1)
162 imgh = RRenderMultiGradient(550, 550, colors, RGRD_VERTICAL);
163 else
164 imgh = RRenderMultiGradient(550, 550, colors, RGRD_DIAGONAL);
166 gettimeofday(&timev, NULL);
167 t2 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
168 total = t2 - t1;
169 printf("gradient rendered in %f sec\n", total);
171 RConvertImage(ctx, imgh, &pix);
172 gettimeofday(&timev, NULL);
173 t1 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
174 total = t1 - t2;
175 rt += total;
176 printf("image converted in %f sec\n", total);
178 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
180 XFlush(dpy);
182 t1 = (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
183 printf("------------------------------------------\n");
184 printf("%i images processed in %f sec\n", i, t1 - t);
185 printf("average time per convertion %f sec\n", rt / i);
186 printf("------------------------------------------\n");
187 #else
188 imgh = RRenderMultiGradient(250, 250, colors, RGRD_HORIZONTAL);
189 imgv = RRenderMultiGradient(250, 250, colors, RGRD_VERTICAL);
190 imgd = RRenderMultiGradient(250, 250, colors, RGRD_DIAGONAL);
191 RConvertImage(ctx, imgh, &pix);
192 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
194 RConvertImage(ctx, imgv, &pix);
195 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 250, 0);
197 RConvertImage(ctx, imgd, &pix);
198 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 500, 0);
200 XFlush(dpy);
201 #endif
203 getchar();
204 return 0;