Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_composite_levels.c « nodes « composite « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1952756d397ab911c52c60d804cd6686cea141e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2006 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Bob Holcomb.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/nodes/composite/nodes/node_composite_levels.c
 *  \ingroup cmpnodes
 */


#include "node_composite_util.h"


/* **************** LEVELS ******************** */
static bNodeSocketTemplate cmp_node_view_levels_in[]= {
	{	SOCK_RGBA, 1, N_("Image"), 0.0f, 0.0f, 0.0f, 1.0f},
	{	-1, 0, ""	}
};

static bNodeSocketTemplate cmp_node_view_levels_out[]={
	{SOCK_FLOAT, 0, N_("Mean")},
	{SOCK_FLOAT, 0, N_("Std Dev")},
	{-1, 0, ""}
};

static void fill_bins(bNode* node, CompBuf* in, int* bins)
{
	float value[4];
	int ivalue=0;
	int x, y;

	/*fill bins */
	for (y=0; y<in->y; y++) {
		for (x=0; x<in->x; x++) {

			/* get the pixel */
			qd_getPixel(in, x, y, value);

			if (value[3] > 0.0f) { /* don't count transparent pixels */
				switch (node->custom1) {
					case 1: { /* all colors */
						value[0] = rgb_to_bw(value);
						value[0]=value[0]*255; /* scale to 0-255 range */
						ivalue=(int)value[0];
						break;
					}
					case 2: { /* red channel */
						value[0]=value[0]*255; /* scale to 0-255 range */
						ivalue=(int)value[0];
						break;
					}
					case 3:  { /* green channel */
						value[1]=value[1]*255; /* scale to 0-255 range */
						ivalue=(int)value[1];
						break;
					}
					case 4: /*blue channel */
					{
						value[2]=value[2]*255; /* scale to 0-255 range */
						ivalue=(int)value[2];
						break;
					}
					case 5: /* luminence */
					{
						rgb_to_yuv(value[0], value[1], value[2], &value[0], &value[1], &value[2]);
						value[0]=value[0]*255; /* scale to 0-255 range */
						ivalue=(int)value[0];
						break;
					}
				} /*end switch */

				/*clip*/
				if (ivalue<0) ivalue=0;
				if (ivalue>255) ivalue=255;

				/*put in the correct bin*/
				bins[ivalue]+=1;
			} /*end if alpha */
		}
	}	
}

static float brightness_mean(bNode* node, CompBuf* in)
{
	float sum=0.0;
	int numPixels=0.0;
	int x, y;
	float value[4];

	for (x=0; x< in->x; x++) {
		for (y=0; y < in->y; y++) {
			
			/* get the pixel */
			qd_getPixel(in, x, y, value);

			if (value[3] > 0.0f) { /* don't count transparent pixels */
				numPixels++;
				switch (node->custom1) {
				case 1:
					{
						value[0] = rgb_to_bw(value);
						sum+=value[0];
						break;
					}
				case 2:
					{
						sum+=value[0];
						break;
					}
				case 3:
					{
						sum+=value[1];
						break;
					}
				case 4:
					{
						sum+=value[2];
						break;
					}
				case 5:
					{
						rgb_to_yuv(value[0], value[1], value[2], &value[0], &value[1], &value[2]);
						sum+=value[0];
						break;
					}
				}
			}
		}
	}

	return sum/numPixels;
}

static float brightness_standard_deviation(bNode* node, CompBuf* in, float mean)
{
	float sum=0.0;
	int numPixels=0.0;
	int x, y;
	float value[4];

	for (x=0; x< in->x; x++) {
		for (y=0; y < in->y; y++) {
			
			/* get the pixel */
			qd_getPixel(in, x, y, value);

			if (value[3] > 0.0f) { /* don't count transparent pixels */
				numPixels++;
				switch (node->custom1) {
				case 1:
					{
						value[0] = rgb_to_bw(value);
						sum+=(value[0]-mean)*(value[0]-mean);
						break;
					}
				case 2:
					{
						sum+=value[0];
						sum+=(value[0]-mean)*(value[0]-mean);
						break;
					}
				case 3:
					{
						sum+=value[1];
						sum+=(value[1]-mean)*(value[1]-mean);
						break;
					}
				case 4:
					{
						sum+=value[2];
						sum+=(value[2]-mean)*(value[2]-mean);
						break;
					}
				case 5:
					{
						rgb_to_yuv(value[0], value[1], value[2], &value[0], &value[1], &value[2]);
						sum+=(value[0]-mean)*(value[0]-mean);
						break;
					}
				}
			}
		}
	}


	return sqrt(sum/(float)(numPixels-1));
}

static void draw_histogram(bNode *node, CompBuf *out, int* bins)
{
	int x, y;
	float color[4]; 
	float value;
	int max;

	/* find max value */
	max=0;
	for (x=0; x<256; x++) {
		if (bins[x]>max) max=bins[x];
	}

	/*draw histogram in buffer */
	for (x=0; x<out->x; x++) {
		for (y=0;y<out->y; y++) {

			/* get normalized value (0..255) */
			value=((float)bins[x]/(float)max)*255.0f;

			if (y < (int)value) { /*if the y value is below the height of the bar for this line then draw with the color */
				switch (node->custom1) {
					case 1: { /* draw in black */
						color[0]=0.0; color[1]=0.0; color[2]=0.0; color[3]=1.0;
						break;
					}
					case 2: { /* draw in red */
						color[0]=1.0; color[1]=0.0; color[2]=0.0; color[3]=1.0;
						break;
					}
					case 3: { /* draw in green */
						color[0]=0.0; color[1]=1.0; color[2]=0.0; color[3]=1.0;
						break;
					}
					case 4: { /* draw in blue */
						color[0]=0.0; color[1]=0.0; color[2]=1.0; color[3]=1.0;
						break;
					}
					case 5: { /* draw in white */
						color[0]=1.0; color[1]=1.0; color[2]=1.0; color[3]=1.0;
						break;
					}
				}
			}
			else {
				color[0]=0.8; color[1]=0.8; color[2]=0.8; color[3]=1.0;
			}

			/* set the color */
			qd_setPixel(out, x, y, color);
		}
	}
}

static void node_composit_exec_view_levels(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
{
	CompBuf* cbuf;
	CompBuf* histogram;
	float mean, std_dev;
	int bins[256];
	int x;

	if (in[0]->hasinput==0)  return;
	if (in[0]->data==NULL) return;

	histogram=alloc_compbuf(256, 256, CB_RGBA, 1);	
	cbuf=typecheck_compbuf(in[0]->data, CB_RGBA);	
		
	/*initalize bins*/
	for (x=0; x<256; x++) {
		bins[x]=0;
	}
	
	/*fill bins */
	fill_bins(node, in[0]->data, bins);

	/* draw the histogram chart */
	draw_histogram(node, histogram, bins);

	/* calculate the average brightness and contrast */
	mean=brightness_mean(node, in[0]->data);
	std_dev=brightness_standard_deviation(node, in[0]->data, mean);

	/*  Printf debuging ;) */
#if 0
	printf("Mean: %f\n", mean);
	printf("Std Dev: %f\n", std_dev);
#endif

	if (out[0]->hasoutput)
			out[0]->vec[0]= mean;
	if (out[1]->hasoutput)
			out[1]->vec[0]= std_dev;

	generate_preview(data, node, histogram);

	if (cbuf!=in[0]->data)
		free_compbuf(cbuf);
	free_compbuf(histogram);
}

static void node_composit_init_view_levels(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
{
	node->custom1=1; /*All channels*/
}

void register_node_type_cmp_view_levels(bNodeTreeType *ttype)
{
	static bNodeType ntype;

	node_type_base(ttype, &ntype, CMP_NODE_VIEW_LEVELS, "Levels", NODE_CLASS_OUTPUT, NODE_OPTIONS|NODE_PREVIEW);
	node_type_socket_templates(&ntype, cmp_node_view_levels_in, cmp_node_view_levels_out);
	node_type_size(&ntype, 140, 100, 320);
	node_type_init(&ntype, node_composit_init_view_levels);
	node_type_storage(&ntype, "ImageUser", NULL, NULL);
	node_type_exec(&ntype, node_composit_exec_view_levels);

	nodeRegisterType(ttype, &ntype);
}