Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / tests / testgrad.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001
9d2e6ef9 scottc1998-09-29 22:36:29 +00002#include <X11/Xlib.h>
9af1c6c4 dan1998-10-21 14:43:47 +00003#include "wraster.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +00004#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
11
12Display *dpy;
13Window win;
14RContext *ctx;
15RImage *imgh, *imgv, *imgd;
16Pixmap pix;
17char *ProgName;
18
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020019void print_help()
9d2e6ef9 scottc1998-09-29 22:36:29 +000020{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020021 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");
9d2e6ef9 scottc1998-09-29 22:36:29 +000027}
28
d82906b4 kojima2000-03-28 08:43:09 +000029#ifdef BENCH
a0ebed9c kojima2000-02-16 18:22:46 +000030#include "bench.h"
d82906b4 kojima2000-03-28 08:43:09 +000031#endif
cb320b2f dan1999-01-11 12:28:12 +000032int main(int argc, char **argv)
9d2e6ef9 scottc1998-09-29 22:36:29 +000033{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020034 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;
9d2e6ef9 scottc1998-09-29 22:36:29 +000041#ifdef BENCH
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020042 double t1, t2, total, t, rt;
43 struct timeval timev;
9d2e6ef9 scottc1998-09-29 22:36:29 +000044#endif
45
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020046 ProgName = strrchr(argv[0], '/');
47 if (!ProgName)
48 ProgName = argv[0];
49 else
50 ProgName++;
51
52 color_name = (char **)malloc(sizeof(char *) * argc);
53 if (color_name == NULL) {
54 fprintf(stderr, "Cannot allocate memory!\n");
55 exit(1);
56 }
57
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);
69 }
70 if (sscanf(argv[i], "%i", &cpc) != 1) {
71 fprintf(stderr, "bad value for colors per channel: \"%s\"\n", argv[i]);
72 exit(0);
73 }
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);
79 }
80 if (sscanf(argv[i], "%i", &visualID) != 1) {
81 fprintf(stderr, "bad value for visual ID: \"%s\"\n", argv[i]);
82 exit(0);
83 }
84 } else if (argv[i][0] != '-') {
85 color_name[ncolors++] = argv[i];
86 } else {
87 print_help();
88 exit(1);
89 }
90 }
91 }
92
93 if (ncolors == 0) {
94 print_help();
95 exit(1);
96 }
97
98 dpy = XOpenDisplay("");
99 if (!dpy) {
100 puts("cant open display");
101 exit(1);
102 }
103 attr.flags = RC_RenderMode | RC_ColorsPerChannel;
104
105 attr.render_mode = rmode;
106 attr.colors_per_channel = cpc;
107
108 if (visualID >= 0) {
109 attr.flags |= RC_VisualID;
110 attr.visualid = visualID;
111 }
112
113 ctx = RCreateContext(dpy, DefaultScreen(dpy), &attr);
114
115 if (!ctx) {
116 printf("could not initialize graphics library context: %s\n", RMessageForError(RErrorCode));
117 exit(1);
118 }
119
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);
131 }
132 }
133 colors[i] = NULL;
134
135 val.background_pixel = ctx->black;
136 val.colormap = ctx->cmap;
137 val.backing_store = Always;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000138#ifdef BENCH
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200139 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 250, 250,
140 0, ctx->depth, InputOutput, ctx->visual,
141 CWColormap | CWBackPixel | CWBackingStore, &val);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000142#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200143 win = XCreateWindow(dpy, DefaultRootWindow(dpy), 10, 10, 750, 250,
144 0, ctx->depth, InputOutput, ctx->visual,
145 CWColormap | CWBackPixel | CWBackingStore, &val);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000146#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200147 XMapRaised(dpy, win);
148 XFlush(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000149
150#ifdef BENCH
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200151 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);
165
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);
170
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);
177
178 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 0, 0);
179
180 XFlush(dpy);
181 }
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");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000187#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200188 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);
a30475fc kojima2000-01-14 16:39:15 +0000193
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200194 RConvertImage(ctx, imgv, &pix);
195 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 250, 0);
a30475fc kojima2000-01-14 16:39:15 +0000196
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200197 RConvertImage(ctx, imgd, &pix);
198 XCopyArea(dpy, pix, win, ctx->copy_gc, 0, 0, 250, 250, 500, 0);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000199
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200200 XFlush(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000201#endif
e7495baf dan1999-02-17 11:06:40 +0000202
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200203 getchar();
204 return 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000205}