Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / grinder / histogram.c
blobd02f175182350b41775dd7d02c93d42902498a2f
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-2015 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include "histogram.h"
25 void histogram_to_png(const GP_Pixmap *src, const char *filename)
27 GP_Histogram *hist;
29 hist = GP_HistogramAlloc(src->pixel_type);
30 if (!hist) {
31 fprintf(stderr, "Failed to allocate histogram\n");
32 return;
35 GP_FilterHistogram(hist, src, NULL);
37 unsigned int i, j;
39 GP_Pixmap *res = GP_PixmapAlloc(257*4, 256, GP_PIXEL_RGB888);
41 GP_Fill(res, 0xffffff);
43 GP_HistogramChannel *hist_r = GP_HistogramChannelByName(hist, "R");
45 for (i = 0; i < hist_r->len; i++)
46 GP_VLineXYH(res, i, 256, -255.00 * hist_r->hist[i] / hist_r->max + 0.5 , 0xff0000);
48 GP_HistogramChannel *hist_g = GP_HistogramChannelByName(hist, "G");
50 for (i = 0; i < hist_g->len; i++)
51 GP_VLineXYH(res, i+257, 256, -255.00 * hist_g->hist[i] / hist_g->max + 0.5 , 0x00ff00);
53 GP_HistogramChannel *hist_b = GP_HistogramChannelByName(hist, "B");
55 for (i = 0; i < hist_b->len; i++)
56 GP_VLineXYH(res, i+514, 256, -255.00 * hist_b->hist[i] / hist_b->max + 0.5 , 0x0000ff);
58 uint32_t max = GP_MAX3(hist_r->max, hist_g->max, hist_b->max);
60 for (i = 0; i < hist_r->len; i++) {
61 for (j = 0; j < hist_r->len; j++) {
62 GP_Pixel pix = 0;
64 if (255 * hist_r->hist[i] / max + 0.5 > j)
65 pix |= 0xff0000;
67 if (255 * hist_g->hist[i] / max + 0.5 > j)
68 pix |= 0x00ff00;
70 if (255 * hist_b->hist[i] / max + 0.5 > j)
71 pix |= 0x0000ff;
73 GP_PutPixel(res, i+771, 256-j, pix);
77 GP_SavePNG(res, filename, NULL);
79 GP_PixmapFree(res);
80 GP_HistogramFree(hist);