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-2011 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
23 #include "histogram.h"
25 void histogram_to_png(const GP_Context
*src
, const char *filename
)
27 GP_FILTER_PARAMS(src
->pixel_type
, params
);
29 GP_FilterHistogramAlloc(src
->pixel_type
, params
);
30 GP_FilterHistogram(src
, params
, NULL
);
34 GP_Context
*res
= GP_ContextAlloc(257*4, 256, GP_PIXEL_RGB888
);
36 GP_Fill(res
, 0xffffff);
39 hist_r
= (GP_FilterParamChannel(params
, "R"))->val
.ptr
;
41 for (i
= 0; i
< hist_r
->len
; i
++)
42 GP_VLineXYH(res
, i
, 256, -255.00 * hist_r
->hist
[i
] / hist_r
->max
+ 0.5 , 0xff0000);
45 hist_g
= (GP_FilterParamChannel(params
, "G"))->val
.ptr
;
47 for (i
= 0; i
< hist_g
->len
; i
++)
48 GP_VLineXYH(res
, i
+257, 256, -255.00 * hist_g
->hist
[i
] / hist_g
->max
+ 0.5 , 0x00ff00);
51 hist_b
= (GP_FilterParamChannel(params
, "B"))->val
.ptr
;
53 for (i
= 0; i
< hist_b
->len
; i
++)
54 GP_VLineXYH(res
, i
+514, 256, -255.00 * hist_b
->hist
[i
] / hist_b
->max
+ 0.5 , 0x0000ff);
56 uint32_t max
= GP_MAX(hist_r
->max
, hist_g
->max
);
58 max
= GP_MAX(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_FilterHistogramFree(params
);