filters/gp_filter_resize_alloc: Check w and h
[gfxprim.git] / demos / c_simple / showimage.c
blobd87491612fe612865de5fef55db4965012bdef20
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Simple example that shows X11 window with image.
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
33 #include <gfxprim.h>
35 int main(int argc, char *argv[])
37 gp_backend *backend;
38 gp_pixmap *image;
40 if (argc != 2) {
41 fprintf(stderr, "Takes image as an argument\n");
42 return 1;
45 /* Load image */
46 image = gp_load_image(argv[1], NULL);
48 if (!image) {
49 fprintf(stderr, "Failed to load bitmap: %s\n", strerror(errno));
50 return 1;
53 /* Initalize backend */
54 backend = gp_x11_init(NULL, 0, 0, image->w, image->h, argv[1], 0);
56 if (!backend) {
57 fprintf(stderr, "Failed to initalize backend\n");
58 return 1;
61 /* Blit image into the window and show it */
62 gp_blit(image, 0, 0, image->w, image->h, backend->pixmap, 0, 0);
63 gp_backend_flip(backend);
65 /* Wait for events */
66 for (;;) {
67 gp_event ev;
69 gp_backend_wait_event(backend, &ev);
71 if (ev.type == GP_EV_KEY && ev.val.val == GP_KEY_Q) {
72 gp_backend_exit(backend);
73 return 0;
76 if (ev.type == GP_EV_SYS && ev.code == GP_EV_SYS_RESIZE) {
77 int cx, cy;
79 gp_backend_resize_ack(backend);
81 cx = ((int)backend->pixmap->w - (int)image->w) / 2;
82 cy = ((int)backend->pixmap->h - (int)image->h) / 2;
84 gp_fill(backend->pixmap, 0);
85 gp_blit_clipped(image, 0, 0, image->w, image->h, backend->pixmap, cx, cy);
86 gp_backend_flip(backend);
90 return 0;