16 RImage
*imgh
, *imgv
, *imgd
;
24 printf("usage: %s [-options] color1 [color2 ...]\n", ProgName
);
26 puts(" -m match colors");
27 puts(" -d dither colors (default)");
28 puts(" -c <cpc> colors per channel to use");
29 puts(" -v <vis-id> visual id to use");
33 int main(int argc
, char **argv
)
35 RContextAttributes attr
;
36 RColor
**colors
= NULL
;
37 int i
, rmode
= RM_DITHER
, ncolors
= 0, cpc
= 4;
40 XSetWindowAttributes val
;
43 double t1
, t2
, total
, t
, rt
;
47 ProgName
= strrchr(argv
[0],'/');
53 color_name
= (char **) malloc(sizeof(char*) * argc
);
54 if(color_name
== NULL
) {
55 fprintf(stderr
, "Cannot allocate memory!\n");
60 for (i
=1; i
<argc
; i
++) {
61 if (strcmp(argv
[i
], "-m")==0) {
63 } else if (strcmp(argv
[i
], "-d")==0) {
65 } else if (strcmp(argv
[i
], "-c")==0) {
68 fprintf(stderr
, "too few arguments for %s\n", argv
[i
-1]);
71 if (sscanf(argv
[i
], "%i", &cpc
)!=1) {
72 fprintf(stderr
, "bad value for colors per channel: \"%s\"\n", argv
[i
]);
75 } else if (strcmp(argv
[i
], "-v")==0) {
78 fprintf(stderr
, "too few arguments for %s\n", argv
[i
-1]);
81 if (sscanf(argv
[i
], "%i", &visualID
)!=1) {
82 fprintf(stderr
, "bad value for visual ID: \"%s\"\n", argv
[i
]);
85 } else if (argv
[i
][0] != '-') {
86 color_name
[ncolors
++] = argv
[i
];
99 dpy
= XOpenDisplay("");
101 puts("cant open display");
104 attr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
;
106 attr
.render_mode
= rmode
;
107 attr
.colors_per_channel
= cpc
;
110 attr
.flags
|= RC_VisualID
;
111 attr
.visualid
= visualID
;
114 ctx
= RCreateContext(dpy
, DefaultScreen(dpy
), &attr
);
117 printf("could not initialize graphics library context: %s\n",
118 RMessageForError(RErrorCode
));
122 colors
= malloc(sizeof(RColor
*)*(ncolors
+1));
123 for (i
=0; i
<ncolors
; i
++) {
124 if (!XParseColor(dpy
, ctx
->cmap
, color_name
[i
], &color
)) {
125 printf("could not parse color \"%s\"\n", color_name
[i
]);
129 colors
[i
] = malloc(sizeof(RColor
));
130 colors
[i
]->red
= color
.red
>> 8;
131 colors
[i
]->green
= color
.green
>> 8;
132 colors
[i
]->blue
= color
.blue
>> 8;
133 printf("0x%02x%02x%02x\n", colors
[i
]->red
, colors
[i
]->green
,
139 val
.background_pixel
= ctx
->black
;
140 val
.colormap
= ctx
->cmap
;
141 val
.backing_store
= Always
;
143 win
= XCreateWindow(dpy
, DefaultRootWindow(dpy
), 10, 10, 250, 250,
144 0, ctx
->depth
, InputOutput
, ctx
->visual
,
145 CWColormap
|CWBackPixel
|CWBackingStore
, &val
);
147 win
= XCreateWindow(dpy
, DefaultRootWindow(dpy
), 10, 10, 750, 250,
148 0, ctx
->depth
, InputOutput
, ctx
->visual
,
149 CWColormap
|CWBackPixel
|CWBackingStore
, &val
);
151 XMapRaised(dpy
, win
);
156 gettimeofday(&timev
, NULL
);
157 t
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
158 for (i
=0; i
<9; i
++) {
160 printf("\nrepeating...\n\n");
161 gettimeofday(&timev
, NULL
);
162 t1
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
164 imgh
= RRenderMultiGradient(250, 250, colors
, RGRD_HORIZONTAL
);
166 imgh
= RRenderMultiGradient(250, 250, colors
, RGRD_VERTICAL
);
168 imgh
= RRenderMultiGradient(250, 250, colors
, RGRD_DIAGONAL
);
170 gettimeofday(&timev
, NULL
);
171 t2
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
173 printf("gradient rendered in %f sec\n", total
);
175 RConvertImage(ctx
, imgh
, &pix
);
176 gettimeofday(&timev
, NULL
);
177 t1
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
180 printf("image converted in %f sec\n", total
);
182 XCopyArea(dpy
, pix
, win
, ctx
->copy_gc
, 0, 0, 250, 250, 0, 0);
186 t1
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
187 printf("------------------------------------------\n");
188 printf("%i images processed in %f sec\n", i
, t1
-t
);
189 printf("average time per convertion %f sec\n", rt
/i
);
190 printf("------------------------------------------\n");
192 imgh
= RRenderMultiGradient(250, 250, colors
, RGRD_HORIZONTAL
);
193 imgv
= RRenderMultiGradient(250, 250, colors
, RGRD_VERTICAL
);
194 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);
198 RConvertImage(ctx
, imgv
, &pix
);
199 XCopyArea(dpy
, pix
, win
, ctx
->copy_gc
, 0, 0, 250, 250, 250, 0);
200 RConvertImage(ctx
, imgd
, &pix
);
201 XCopyArea(dpy
, pix
, win
, ctx
->copy_gc
, 0, 0, 250, 250, 500, 0);