backends: X11, Add CREATE_ROOT mode.
[gfxprim.git] / libs / filters / GP_Stats.c
blobdf65794f869a14f95bc1c87cb0145222624a0cbd
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-2011 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <string.h>
25 #include <GP_Debug.h>
27 #include "GP_Stats.h"
29 int GP_FilterHistogram_Raw(const GP_Context *src, GP_FilterParam histogram[],
30 GP_ProgressCallback *callback);
32 int GP_FilterHistogram(const GP_Context *src, GP_FilterParam histogram[],
33 GP_ProgressCallback *callback)
35 int ret;
37 ret = GP_FilterHistogram_Raw(src, histogram, callback);
39 if (ret)
40 return ret;
42 unsigned int i;
44 for (i = 0; histogram[i].channel_name[0] != '\0'; i++) {
45 unsigned int j;
46 GP_Histogram *hist = histogram[i].val.ptr;
48 hist->max = hist->hist[0];
49 hist->min = hist->hist[0];
51 for (j = 1; j < hist->len; j++) {
52 if (hist->hist[j] > hist->max)
53 hist->max = hist->hist[j];
55 if (hist->hist[j] < hist->min)
56 hist->min = hist->hist[j];
60 return 0;
63 void GP_FilterHistogramAlloc(GP_PixelType type, GP_FilterParam params[])
65 uint32_t i;
67 GP_FilterParamSetPtrAll(params, NULL);
69 const GP_PixelTypeChannel *channels = GP_PixelTypes[type].channels;
71 for (i = 0; i < GP_PixelTypes[type].numchannels; i++) {
72 size_t chan_size = 1<<channels[i].size;
74 GP_Histogram *hist = malloc(sizeof(struct GP_Histogram) +
75 sizeof(uint32_t) * chan_size);
77 if (hist == NULL) {
78 GP_FilterHistogramFree(params);
79 return;
82 hist->len = chan_size;
83 memset(hist->hist, 0, sizeof(uint32_t) * chan_size);
85 (GP_FilterParamChannel(params, channels[i].name))->val.ptr = hist;