1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2009-2015 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
23 #include "histogram.h"
25 void histogram_to_png(const GP_Pixmap
*src
, const char *filename
)
29 hist
= GP_HistogramAlloc(src
->pixel_type
);
31 fprintf(stderr
, "Failed to allocate histogram\n");
35 GP_FilterHistogram(hist
, src
, NULL
);
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
++) {
64 if (255 * hist_r
->hist
[i
] / max
+ 0.5 > j
)
67 if (255 * hist_g
->hist
[i
] / max
+ 0.5 > j
)
70 if (255 * hist_b
->hist
[i
] / max
+ 0.5 > j
)
73 GP_PutPixel(res
, i
+771, 256-j
, pix
);
77 GP_SavePNG(res
, filename
, NULL
);
80 GP_HistogramFree(hist
);