17 RImage
*imgh
, *imgv
, *imgd
;
25 printf("usage: %s [-options] color1 [color2 ...]\n", ProgName
);
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;
41 XSetWindowAttributes val
;
44 double t1
, t2
, total
, t
, rt
;
48 ProgName
= strrchr(argv
[0],'/');
54 color_name
= (char **) malloc(sizeof(char*) * argc
);
55 if(color_name
== NULL
) {
56 fprintf(stderr
, "Cannot allocate memory!\n");
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) {
69 fprintf(stderr
, "too few arguments for %s\n", argv
[i
-1]);
72 if (sscanf(argv
[i
], "%i", &cpc
)!=1) {
73 fprintf(stderr
, "bad value for colors per channel: \"%s\"\n", argv
[i
]);
76 } else if (strcmp(argv
[i
], "-v")==0) {
79 fprintf(stderr
, "too few arguments for %s\n", argv
[i
-1]);
82 if (sscanf(argv
[i
], "%i", &visualID
)!=1) {
83 fprintf(stderr
, "bad value for visual ID: \"%s\"\n", argv
[i
]);
86 } else if (argv
[i
][0] != '-') {
87 color_name
[ncolors
++] = argv
[i
];
100 dpy
= XOpenDisplay("");
102 puts("cant open display");
105 attr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
;
107 attr
.render_mode
= rmode
;
108 attr
.colors_per_channel
= cpc
;
111 attr
.flags
|= RC_VisualID
;
112 attr
.visualid
= visualID
;
115 ctx
= RCreateContext(dpy
, DefaultScreen(dpy
), &attr
);
118 printf("could not initialize graphics library context: %s\n",
119 RMessageForError(RErrorCode
));
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
]);
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
,
140 val
.background_pixel
= ctx
->black
;
141 val
.colormap
= ctx
->cmap
;
142 val
.backing_store
= Always
;
144 win
= XCreateWindow(dpy
, DefaultRootWindow(dpy
), 10, 10, 250, 250,
145 0, ctx
->depth
, InputOutput
, ctx
->visual
,
146 CWColormap
|CWBackPixel
|CWBackingStore
, &val
);
148 win
= XCreateWindow(dpy
, DefaultRootWindow(dpy
), 10, 10, 750, 250,
149 0, ctx
->depth
, InputOutput
, ctx
->visual
,
150 CWColormap
|CWBackPixel
|CWBackingStore
, &val
);
152 XMapRaised(dpy
, win
);
157 gettimeofday(&timev
, NULL
);
158 t
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
159 for (i
=0; i
<9; i
++) {
161 printf("\nrepeating...\n\n");
162 gettimeofday(&timev
, NULL
);
163 t1
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
165 imgh
= RRenderMultiGradient(550, 550, colors
, RGRD_HORIZONTAL
);
167 imgh
= RRenderMultiGradient(550, 550, colors
, RGRD_VERTICAL
);
169 imgh
= RRenderMultiGradient(550, 550, colors
, RGRD_DIAGONAL
);
171 gettimeofday(&timev
, NULL
);
172 t2
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
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);
181 printf("image converted in %f sec\n", total
);
183 XCopyArea(dpy
, pix
, win
, ctx
->copy_gc
, 0, 0, 250, 250, 0, 0);
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");
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);