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");
36 int main(int argc
, char **argv
)
38 RContextAttributes attr
;
39 RColor
**colors
= NULL
;
40 int i
, rmode
= RDitheredRendering
, ncolors
= 0, cpc
= 4;
43 XSetWindowAttributes val
;
46 double t1
, t2
, total
, t
, rt
;
50 ProgName
= strrchr(argv
[0],'/');
56 color_name
= (char **) malloc(sizeof(char*) * argc
);
57 if(color_name
== NULL
) {
58 fprintf(stderr
, "Cannot allocate memory!\n");
63 for (i
=1; i
<argc
; i
++) {
64 if (strcmp(argv
[i
], "-m")==0) {
65 rmode
= RBestMatchRendering
;
66 } else if (strcmp(argv
[i
], "-d")==0) {
67 rmode
= RDitheredRendering
;
68 } else if (strcmp(argv
[i
], "-c")==0) {
71 fprintf(stderr
, "too few arguments for %s\n", argv
[i
-1]);
74 if (sscanf(argv
[i
], "%i", &cpc
)!=1) {
75 fprintf(stderr
, "bad value for colors per channel: \"%s\"\n", argv
[i
]);
78 } else if (strcmp(argv
[i
], "-v")==0) {
81 fprintf(stderr
, "too few arguments for %s\n", argv
[i
-1]);
84 if (sscanf(argv
[i
], "%i", &visualID
)!=1) {
85 fprintf(stderr
, "bad value for visual ID: \"%s\"\n", argv
[i
]);
88 } else if (argv
[i
][0] != '-') {
89 color_name
[ncolors
++] = argv
[i
];
102 dpy
= XOpenDisplay("");
104 puts("cant open display");
107 attr
.flags
= RC_RenderMode
| RC_ColorsPerChannel
;
109 attr
.render_mode
= rmode
;
110 attr
.colors_per_channel
= cpc
;
113 attr
.flags
|= RC_VisualID
;
114 attr
.visualid
= visualID
;
117 ctx
= RCreateContext(dpy
, DefaultScreen(dpy
), &attr
);
120 printf("could not initialize graphics library context: %s\n",
121 RMessageForError(RErrorCode
));
125 colors
= malloc(sizeof(RColor
*)*(ncolors
+1));
126 for (i
=0; i
<ncolors
; i
++) {
127 if (!XParseColor(dpy
, ctx
->cmap
, color_name
[i
], &color
)) {
128 printf("could not parse color \"%s\"\n", color_name
[i
]);
132 colors
[i
] = malloc(sizeof(RColor
));
133 colors
[i
]->red
= color
.red
>> 8;
134 colors
[i
]->green
= color
.green
>> 8;
135 colors
[i
]->blue
= color
.blue
>> 8;
136 printf("0x%02x%02x%02x\n", colors
[i
]->red
, colors
[i
]->green
,
142 val
.background_pixel
= ctx
->black
;
143 val
.colormap
= ctx
->cmap
;
144 val
.backing_store
= Always
;
146 win
= XCreateWindow(dpy
, DefaultRootWindow(dpy
), 10, 10, 250, 250,
147 0, ctx
->depth
, InputOutput
, ctx
->visual
,
148 CWColormap
|CWBackPixel
|CWBackingStore
, &val
);
150 win
= XCreateWindow(dpy
, DefaultRootWindow(dpy
), 10, 10, 750, 250,
151 0, ctx
->depth
, InputOutput
, ctx
->visual
,
152 CWColormap
|CWBackPixel
|CWBackingStore
, &val
);
154 XMapRaised(dpy
, win
);
159 gettimeofday(&timev
, NULL
);
160 t
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
161 for (i
=0; i
<9; i
++) {
163 printf("\nrepeating...\n\n");
164 gettimeofday(&timev
, NULL
);
165 t1
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
167 imgh
= RRenderMultiGradient(550, 550, colors
, RGRD_HORIZONTAL
);
169 imgh
= RRenderMultiGradient(550, 550, colors
, RGRD_VERTICAL
);
171 imgh
= RRenderMultiGradient(550, 550, colors
, RGRD_DIAGONAL
);
173 gettimeofday(&timev
, NULL
);
174 t2
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
176 printf("gradient rendered in %f sec\n", total
);
178 RConvertImage(ctx
, imgh
, &pix
);
179 gettimeofday(&timev
, NULL
);
180 t1
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
183 printf("image converted in %f sec\n", total
);
185 XCopyArea(dpy
, pix
, win
, ctx
->copy_gc
, 0, 0, 250, 250, 0, 0);
189 t1
= (double)timev
.tv_sec
+ (((double)timev
.tv_usec
)/1000000);
190 printf("------------------------------------------\n");
191 printf("%i images processed in %f sec\n", i
, t1
-t
);
192 printf("average time per convertion %f sec\n", rt
/i
);
193 printf("------------------------------------------\n");
195 imgh
= RRenderMultiGradient(250, 250, colors
, RGRD_HORIZONTAL
);
196 imgv
= RRenderMultiGradient(250, 250, colors
, RGRD_VERTICAL
);
197 imgd
= RRenderMultiGradient(250, 250, colors
, RGRD_DIAGONAL
);
198 RConvertImage(ctx
, imgh
, &pix
);
199 XCopyArea(dpy
, pix
, win
, ctx
->copy_gc
, 0, 0, 250, 250, 0, 0);
201 RConvertImage(ctx
, imgv
, &pix
);
202 XCopyArea(dpy
, pix
, win
, ctx
->copy_gc
, 0, 0, 250, 250, 250, 0);
204 RConvertImage(ctx
, imgd
, &pix
);
205 XCopyArea(dpy
, pix
, win
, ctx
->copy_gc
, 0, 0, 250, 250, 500, 0);