Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / convolution.c
blob7d26f17563f22808deeb52fa82ae1db0d0f82c91
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-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Convolution filter example.
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
33 #include <GP.h>
35 struct callback_priv {
36 char *op;
37 char *name;
40 static int progress_callback(GP_ProgressCallback *self)
42 struct callback_priv *priv = self->priv;
44 printf("\r%s '%s' %3.1f%%", priv->op, priv->name, self->percentage);
45 fflush(stdout);
48 * It's important to return zero as non-zero return value
49 * aborts the operation.
51 return 0;
54 int main(int argc, char *argv[])
56 GP_Pixmap *img;
57 struct callback_priv priv;
58 GP_ProgressCallback callback = {.callback = progress_callback,
59 .priv = &priv};
61 if (argc != 2) {
62 fprintf(stderr, "Takes an image as an parameter\n");
63 return 1;
66 priv.op = "Loading";
67 priv.name = argv[1];
69 img = GP_LoadImage(argv[1], &callback);
71 if (img == NULL) {
72 fprintf(stderr, "Failed to load image '%s': %s\n", argv[1],
73 strerror(errno));
74 return 1;
77 printf("\n");
80 GP_FilterKernel2D box = {
81 .w = 5,
82 .h = 5,
83 .div = 11.8,
84 .kernel = (float[]) {
85 0.0, 0.1, 1.0, 0.1, 0.0,
86 0.1, 0.5, 1.0, 0.5, 0.1,
87 1.0, 1.0, 1.0, 1.0, 1.0,
88 0.1, 0.5, 1.0, 0.5, 0.1,
89 0.0, 0.1, 1.0, 0.1, 0.0,
93 priv.op = "Linear Convolution";
96 * Blur in-place, inner rectangle of the image.
98 GP_FilterConvolutionEx(img, img->w/4, img->h/4, img->w/2, img->h/2,
99 img, img->w/4, img->h/4, &box, &callback);
101 printf("\n");
103 priv.op = "Saving";
104 priv.name = "out.png";
106 if (GP_SavePNG(img, "out.png", &callback)) {
107 fprintf(stderr, "Failed to save image: %s", strerror(errno));
108 return 1;
111 printf("\n");
113 return 0;