filters/gp_filter_resize_alloc: Check w and h
[gfxprim.git] / demos / c_simple / memory_io.c
blob43aa53f805f7d28e906f96039475702862e296f6
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-2014 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Simple memory IO example.
29 #include <stdio.h>
30 #include <gfxprim.h>
33 * Binary PGM stored in an array
35 static char pgm[] = {
36 /* header */
37 'P', '5', '\n',
38 '1', '0', ' ', '1', '0', '\n',
39 '2', '5', '5', '\n',
40 /* data */
41 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
42 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
43 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
44 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
45 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
46 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
47 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
48 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
49 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
50 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
53 #define WIN_W 100
54 #define WIN_H 100
56 int main(void)
58 gp_backend *b;
59 gp_pixmap *img;
60 gp_io *io;
62 io = gp_io_mem(pgm, sizeof(pgm), NULL);
64 if (!io) {
65 fprintf(stderr, "Failed to initialize IO\n");
66 return 1;
69 img = gp_read_pgm(io, NULL);
70 gp_io_close(io);
72 if (!img) {
73 fprintf(stderr, "Failed to load image\n");
74 return 1;
77 b = gp_x11_init(NULL, 0, 0, WIN_W, WIN_H, "IO Example", 0);
79 if (!b) {
80 fprintf(stderr, "Failed to initialize backend\n");
81 return 1;
84 gp_fill(b->pixmap, 0);
85 gp_blit_clipped(img, 0, 0, img->w, img->h, b->pixmap,
86 (WIN_W - img->w)/2, (WIN_H - img->h)/2);
87 gp_backend_flip(b);
89 for (;;) {
90 gp_event ev;
92 gp_backend_wait_event(b, &ev);
94 switch (ev.type) {
95 case GP_EV_KEY:
96 switch (ev.val.val) {
97 case GP_KEY_ESC:
98 case GP_KEY_Q:
99 gp_backend_exit(b);
100 return 0;
101 break;
103 break;
104 case GP_EV_SYS:
105 switch (ev.code) {
106 case GP_EV_SYS_RESIZE:
107 case GP_EV_SYS_QUIT:
108 gp_backend_exit(b);
109 return 0;
110 break;
112 break;
116 gp_backend_exit(b);
117 return 0;