Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / weighted_median.c
blobdc481603f9d5ecb115c49fe8a4cde41f6e4ec777
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");
79 unsigned int circle[] = {
80 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
81 0, 0, 1, 1, 1, 1, 1, 1, 1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 1, 0, 0, 0, 0, 0, 1, 0, 0,
117 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,
118 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
121 unsigned int crazy[] = {
122 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
123 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1,
124 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
125 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
126 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
127 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
128 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
129 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
130 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
131 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
132 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1,
133 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
136 GP_MedianWeights weights = {
137 .w = 11,
138 .h = 11,
139 .weights = circle,
142 priv.op = "Weighted Median";
144 GP_Pixmap *res = GP_FilterWeightedMedianAlloc(img, &weights, &callback);
146 printf("\n");
148 priv.op = "Saving";
149 priv.name = "out.png";
151 if (GP_SavePNG(res, "out.png", &callback)) {
152 fprintf(stderr, "Failed to save image: %s", strerror(errno));
153 return 1;
156 printf("\n");
158 return 0;