Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * Copyright © 2009 Chris Wilson |
| 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 6 | * documentation for any purpose is hereby granted without fee, provided that |
| 7 | * the above copyright notice appear in all copies and that both that copyright |
| 8 | * notice and this permission notice appear in supporting documentation, and |
| 9 | * that the name of the copyright holders not be used in advertising or |
| 10 | * publicity pertaining to distribution of the software without specific, |
| 11 | * written prior permission. The copyright holders make no representations |
| 12 | * about the suitability of this software for any purpose. It is provided "as |
| 13 | * is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 21 | * OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <unistd.h> |
| 30 | #include <math.h> |
| 31 | #include <time.h> |
| 32 | #include <cairo.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 33 | #include <glib.h> |
| 34 | #include <gdk-pixbuf/gdk-pixbuf.h> |
| 35 | |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 36 | #include <wayland-client.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 37 | |
| 38 | #include "window.h" |
| 39 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 40 | struct image { |
| 41 | struct window *window; |
| 42 | struct display *display; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 43 | gchar *filename; |
| 44 | }; |
| 45 | |
| 46 | static void |
| 47 | set_source_pixbuf(cairo_t *cr, |
| 48 | const GdkPixbuf *pixbuf, |
| 49 | double src_x, |
| 50 | double src_y, |
| 51 | double src_width, |
| 52 | double src_height) |
| 53 | { |
| 54 | gint width = gdk_pixbuf_get_width (pixbuf); |
| 55 | gint height = gdk_pixbuf_get_height (pixbuf); |
| 56 | guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf); |
| 57 | int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
| 58 | int n_channels = gdk_pixbuf_get_n_channels (pixbuf); |
| 59 | int cairo_stride; |
| 60 | guchar *cairo_pixels; |
| 61 | cairo_format_t format; |
| 62 | cairo_surface_t *surface; |
| 63 | int j; |
| 64 | |
| 65 | if (n_channels == 3) |
| 66 | format = CAIRO_FORMAT_RGB24; |
| 67 | else |
| 68 | format = CAIRO_FORMAT_ARGB32; |
| 69 | |
| 70 | surface = cairo_image_surface_create(format, width, height); |
| 71 | if (cairo_surface_status(surface)) { |
| 72 | cairo_set_source_surface(cr, surface, 0, 0); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | cairo_stride = cairo_image_surface_get_stride(surface); |
| 77 | cairo_pixels = cairo_image_surface_get_data(surface); |
| 78 | |
| 79 | for (j = height; j; j--) { |
| 80 | guchar *p = gdk_pixels; |
| 81 | guchar *q = cairo_pixels; |
| 82 | |
| 83 | if (n_channels == 3) { |
| 84 | guchar *end = p + 3 * width; |
| 85 | |
| 86 | while (p < end) { |
| 87 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
| 88 | q[0] = p[2]; |
| 89 | q[1] = p[1]; |
| 90 | q[2] = p[0]; |
| 91 | #else |
| 92 | q[1] = p[0]; |
| 93 | q[2] = p[1]; |
| 94 | q[3] = p[2]; |
| 95 | #endif |
| 96 | p += 3; |
| 97 | q += 4; |
| 98 | } |
| 99 | } else { |
| 100 | guchar *end = p + 4 * width; |
| 101 | guint t1,t2,t3; |
| 102 | |
| 103 | #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END |
| 104 | |
| 105 | while (p < end) { |
| 106 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
| 107 | MULT(q[0], p[2], p[3], t1); |
| 108 | MULT(q[1], p[1], p[3], t2); |
| 109 | MULT(q[2], p[0], p[3], t3); |
| 110 | q[3] = p[3]; |
| 111 | #else |
| 112 | q[0] = p[3]; |
| 113 | MULT(q[1], p[0], p[3], t1); |
| 114 | MULT(q[2], p[1], p[3], t2); |
| 115 | MULT(q[3], p[2], p[3], t3); |
| 116 | #endif |
| 117 | |
| 118 | p += 4; |
| 119 | q += 4; |
| 120 | } |
| 121 | #undef MULT |
| 122 | } |
| 123 | |
| 124 | gdk_pixels += gdk_rowstride; |
| 125 | cairo_pixels += cairo_stride; |
| 126 | } |
| 127 | cairo_surface_mark_dirty(surface); |
| 128 | |
| 129 | cairo_set_source_surface(cr, surface, |
| 130 | src_x + .5 * (src_width - width), |
| 131 | src_y + .5 * (src_height - height)); |
| 132 | cairo_surface_destroy(surface); |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | image_draw(struct image *image) |
| 137 | { |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 138 | struct rectangle allocation; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 139 | GdkPixbuf *pb; |
| 140 | cairo_t *cr; |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 141 | cairo_surface_t *surface; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 142 | |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 143 | window_get_child_allocation(image->window, &allocation); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 144 | |
| 145 | pb = gdk_pixbuf_new_from_file_at_size(image->filename, |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 146 | allocation.width, |
| 147 | allocation.height, |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 148 | NULL); |
| 149 | if (pb == NULL) |
| 150 | return; |
| 151 | |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 152 | surface = window_get_surface(image->window); |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 153 | cr = cairo_create(surface); |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 154 | window_get_child_allocation(image->window, &allocation); |
| 155 | cairo_rectangle(cr, allocation.x, allocation.y, |
| 156 | allocation.width, allocation.height); |
| 157 | cairo_clip(cr); |
| 158 | cairo_push_group(cr); |
| 159 | cairo_translate(cr, allocation.x, allocation.y); |
| 160 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 161 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 162 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
| 163 | cairo_paint(cr); |
| 164 | set_source_pixbuf(cr, pb, |
| 165 | 0, 0, |
Kristian Høgsberg | da846ca | 2011-01-11 10:00:52 -0500 | [diff] [blame] | 166 | allocation.width, allocation.height); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 167 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 168 | cairo_paint(cr); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 169 | |
| 170 | g_object_unref(pb); |
| 171 | |
Kristian Høgsberg | e164e4e | 2011-01-21 11:35:05 -0500 | [diff] [blame] | 172 | cairo_pop_group_to_source(cr); |
| 173 | cairo_paint(cr); |
| 174 | cairo_destroy(cr); |
| 175 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 176 | cairo_surface_destroy(surface); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 179 | static void |
| 180 | redraw_handler(struct window *window, void *data) |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 181 | { |
| 182 | struct image *image = data; |
| 183 | |
| 184 | image_draw(image); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 188 | keyboard_focus_handler(struct window *window, |
Kristian Høgsberg | 43788b1 | 2010-07-28 23:50:12 -0400 | [diff] [blame] | 189 | struct input *device, void *data) |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 190 | { |
| 191 | struct image *image = data; |
| 192 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 193 | window_schedule_redraw(image->window); |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 196 | static struct image * |
Kristian Høgsberg | cadd0f5 | 2012-01-09 18:56:37 -0500 | [diff] [blame] | 197 | image_create(struct display *display, const char *filename) |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 198 | { |
| 199 | struct image *image; |
| 200 | gchar *basename; |
| 201 | gchar *title; |
| 202 | |
| 203 | image = malloc(sizeof *image); |
| 204 | if (image == NULL) |
| 205 | return image; |
| 206 | memset(image, 0, sizeof *image); |
| 207 | |
| 208 | basename = g_path_get_basename(filename); |
| 209 | title = g_strdup_printf("Wayland Image - %s", basename); |
| 210 | g_free(basename); |
| 211 | |
| 212 | image->filename = g_strdup(filename); |
| 213 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 214 | image->window = window_create(display, 500, 400); |
| 215 | window_set_title(image->window, title); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 216 | image->display = display; |
| 217 | |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 218 | window_set_user_data(image->window, image); |
| 219 | window_set_redraw_handler(image->window, redraw_handler); |
Kristian Høgsberg | 43788b1 | 2010-07-28 23:50:12 -0400 | [diff] [blame] | 220 | window_set_keyboard_focus_handler(image->window, |
| 221 | keyboard_focus_handler); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 222 | |
Kristian Høgsberg | 5d12990 | 2012-01-10 10:49:41 -0500 | [diff] [blame^] | 223 | window_schedule_redraw(image->window); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 224 | |
| 225 | return image; |
| 226 | } |
| 227 | |
| 228 | static const GOptionEntry option_entries[] = { |
| 229 | { NULL } |
| 230 | }; |
| 231 | |
| 232 | int |
| 233 | main(int argc, char *argv[]) |
| 234 | { |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 235 | struct display *d; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 236 | int i; |
| 237 | |
Kristian Høgsberg | 9de79a9 | 2011-08-24 11:09:53 -0400 | [diff] [blame] | 238 | d = display_create(&argc, &argv, option_entries); |
Yuval Fledel | e9f5e36 | 2010-11-22 21:34:19 +0200 | [diff] [blame] | 239 | if (d == NULL) { |
| 240 | fprintf(stderr, "failed to create display: %m\n"); |
| 241 | return -1; |
| 242 | } |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 243 | |
Kristian Høgsberg | 0043961 | 2011-01-25 15:16:01 -0500 | [diff] [blame] | 244 | for (i = 1; i < argc; i++) |
Kristian Høgsberg | cadd0f5 | 2012-01-09 18:56:37 -0500 | [diff] [blame] | 245 | image_create (d, argv[i]); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 246 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 247 | display_run(d); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 248 | |
| 249 | return 0; |
| 250 | } |