15 RImage
*imgh
, *imgv
, *imgd
;
21 printf("usage: %s [-options] color1 [color2 ...]\n", ProgName
);
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");
32 int main(int argc
, char **argv
)
34 RContextAttributes attr
;
35 RColor
**colors
= NULL
;
36 int i
, rmode
= RDitheredRendering
, ncolors
= 0, cpc
= 4;
39 XSetWindowAttributes val
;
42 double t1
, t2
, total
, t
, rt
;
46 ProgName
= strrchr(argv
[0], '/');
52 color_name
= (char **)malloc(sizeof(char *) * argc
);
53 if (color_name
== NULL
) {
54 fprintf(stderr
, "Cannot allocate memory!\n");
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) {
67 fprintf(stderr
, "too few arguments for %s\n", argv
[i
- 1]);
70 if (sscanf(argv
[i
], "%i", &cpc
) != 1) {
71 fprintf(stderr
, "bad value for colors per channel: \"%s\"\n", argv
[i
]);
74 } else if (strcmp(argv
[i
], "-v") == 0) {
77 fprintf(stderr
, "too few arguments for %s\n", argv
[i
- 1]);
80 if (sscanf(argv
[i
], "%i", &visualID
) != 1) {
81 fprintf(stderr
, "bad value for visual ID: \"%s\"\n", argv
[i
]);
84 } else if (argv
[i
][0] != '-') {
85 color_name
[ncolors
++] = argv
[i
];
98 dpy
= XOpenDisplay("");
100 puts("cant open display");
103 attr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
;
105 attr
.render_mode
= rmode
;
106 attr
.colors_per_channel
= cpc
;
109 attr
.flags
|= RC_VisualID
;
110 attr
.visualid
= visualID
;
113 ctx
= RCreateContext(dpy
, DefaultScreen(dpy
), &attr
);
116 printf("could not initialize graphics library context: %s\n", RMessageForError(RErrorCode
));
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
]);
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
);
135 val
.background_pixel
= ctx
->black
;
136 val
.colormap
= ctx
->cmap
;
137 val
.backing_store
= Always
;
139 win
= XCreateWindow(dpy
, DefaultRootWindow(dpy
), 10, 10, 250, 250,
140 0, ctx
->depth
, InputOutput
, ctx
->visual
,
141 CWColormap
| CWBackPixel
| CWBackingStore
, &val
);
143 win
= XCreateWindow(dpy
, DefaultRootWindow(dpy
), 10, 10, 750, 250,
144 0, ctx
->depth
, InputOutput
, ctx
->visual
,
145 CWColormap
| CWBackPixel
| CWBackingStore
, &val
);
147 XMapRaised(dpy
, win
);
152 gettimeofday(&timev
, NULL
);
153 t
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
) / 1000000);
154 for (i
= 0; i
< 9; i
++) {
156 printf("\nrepeating...\n\n");
157 gettimeofday(&timev
, NULL
);
158 t1
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
) / 1000000);
160 imgh
= RRenderMultiGradient(550, 550, colors
, RGRD_HORIZONTAL
);
162 imgh
= RRenderMultiGradient(550, 550, colors
, RGRD_VERTICAL
);
164 imgh
= RRenderMultiGradient(550, 550, colors
, RGRD_DIAGONAL
);
166 gettimeofday(&timev
, NULL
);
167 t2
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
) / 1000000);
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);
176 printf("image converted in %f sec\n", total
);
178 XCopyArea(dpy
, pix
, win
, ctx
->copy_gc
, 0, 0, 250, 250, 0, 0);
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");
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);