2 * Copyright © 2008 Bart Massey <bart@cs.pdx.edu>
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Except as contained in this notice, the names of the authors or
24 * their institutions shall not be used in advertising or otherwise to
25 * promote the sale, use or other dealings in this Software without
26 * prior written authorization from the authors.
29 /* gcc -g -O2 -Wall `pkg-config --cflags --libs xcb` -o test xcb_image.o test_xcb_image.c */
36 #include "../aux/xcb_aux.h"
37 #include "xcb_image.h"
43 reflect_window (xcb_connection_t *c,
45 xcb_drawable_t new_win,
58 format = XCB_IMAGE_FORMAT_Z_PIXMAP;
60 printf ("get_image %d %d\n", width, height);
61 image = xcb_image_get (c, win,
66 printf ("Create image summary:\n");
67 printf (" * format................: %d\n", image->format);
68 printf (" * byte order............: %d\n", image->byte_order);
69 printf (" * bitmap order..........: %d\n", image->bit_order);
70 printf (" * bitmap pad............: %d\n", image->scanline_pad);
71 printf (" * depth.................: %d\n", image->depth);
72 printf (" * bytes/line............: %d\n", image->stride);
73 printf (" * bits/pixel (or unit)..: %d\n", image->bpp);
75 printf ("bpl %d %d\n", image->stride, image->height);
77 printf("calculating reflection -- this may take awhile...\n");
79 for (left_x = 0 ; left_x < width/2 ; left_x++)
81 for (y = 0 ; y < height ; y++)
83 pixel1 = xcb_image_get_pixel (image, left_x, y);
84 right_x = width - left_x-1;
85 if (left_x != right_x)
87 pixel2 = xcb_image_get_pixel (image, right_x, y);
88 xcb_image_put_pixel (image, left_x, y, pixel2);
90 xcb_image_put_pixel (image, right_x, y, pixel1);
94 printf("putting image\n");
96 xcb_image_put (c, new_win, gc, image, 0, 0, 0);
97 image = xcb_image_get (c, new_win,
106 main (int argc, char *argv[])
109 xcb_screen_t *screen;
111 xcb_drawable_t new_win;
113 xcb_rectangle_t rect_coord = { 0, 0, W_W, W_H};
114 xcb_gcontext_t bgcolor, fgcolor;
115 xcb_point_t points[2];
121 xcb_generic_event_t *e;
123 /* Open the connexion to the X server and get the first screen */
124 c = xcb_connect (NULL, &screen_nbr);
125 screen = xcb_aux_get_screen (c, screen_nbr);
126 depth = xcb_aux_get_depth (c, screen);
128 /* Create a black graphic context for drawing in the foreground */
131 fgcolor = xcb_generate_id(c);
132 mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
133 valgc[0] = screen->black_pixel;
134 valgc[1] = 0; /* no graphics exposures */
135 xcb_create_gc(c, fgcolor, win, mask, valgc);
137 bgcolor = xcb_generate_id(c);
138 mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
139 valgc[0] = screen->white_pixel;
140 valgc[1] = 0; /* no graphics exposures */
141 xcb_create_gc(c, bgcolor, win, mask, valgc);
143 /* Ask for our window's Id */
144 win = xcb_generate_id(c);
146 /* Create the window */
147 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE;
148 valwin[0] = screen->white_pixel;
149 valwin[1] = XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_EXPOSURE;
150 valwin[2] = XCB_EVENT_MASK_BUTTON_PRESS;
151 xcb_create_window (c, /* Connection */
154 screen->root, /* parent window */
156 W_W, W_H, /* width, height */
157 10, /* border_width */
158 XCB_WINDOW_CLASS_INPUT_OUTPUT,/* class */
159 screen->root_visual, /* visual */
160 mask, valwin); /* masks, not used yet */
162 /* Map the window on the screen */
163 xcb_map_window (c, win);
165 /* Create a Pixmap that will fill the window */
166 rect = xcb_generate_id (c);
167 xcb_create_pixmap(c, depth, rect, win, W_W, W_H);
168 xcb_poly_fill_rectangle(c, rect, bgcolor, 1, &rect_coord);
173 xcb_poly_line(c, XCB_COORD_MODE_ORIGIN, rect, fgcolor, 2, points);
174 points[0].x = W_W / 4;
176 points[1].x = W_W / 2;
177 points[1].y = W_H / 2;
178 xcb_poly_line(c, XCB_COORD_MODE_ORIGIN, rect, fgcolor, 2, points);
180 /* Ask for our window's Id */
181 new_win = xcb_generate_id(c);
183 /* Create the window */
184 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE;
185 valwin[0] = screen->white_pixel;
186 valwin[1] = XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_EXPOSURE;
187 valwin[2] = XCB_EVENT_MASK_BUTTON_PRESS;
188 xcb_create_window (c, /* Connection */
190 new_win, /* window Id */
191 screen->root, /* parent window */
193 W_W, W_H, /* width, height */
194 10, /* border_width */
195 XCB_WINDOW_CLASS_INPUT_OUTPUT,/* class */
196 screen->root_visual, /* visual */
197 mask, valwin); /* masks, not used yet */
201 /* Map the window on the screen */
202 xcb_map_window (c, new_win);
207 while ((e = xcb_wait_for_event(c)))
209 switch (e->response_type)
213 xcb_copy_area(c, rect, win, bgcolor,
214 0, 0, 0, 0, W_W, W_H);
215 reflect_window (c, win, new_win,