File: pix_mean_color.cpp

package info (click to toggle)
gem 1%3A0.93.3-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,548 kB
  • ctags: 29,332
  • sloc: cpp: 134,188; sh: 11,215; makefile: 2,853; ansic: 84
file content (156 lines) | stat: -rw-r--r-- 4,240 bytes parent folder | download | duplicates (3)
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
// Copyright (c) 2004 Tim Blechmann
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "GEM.LICENSE.TERMS"  in this distribution.
//
// 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.
//
// See file GEM.LICENSE.TERMS for further informations on licensing terms.
//
// 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include "pix_mean_color.h"


CPPEXTERN_NEW_WITH_GIMME(pix_mean_color);

/* const, destructor */
pix_mean_color::pix_mean_color(int argc, t_atom *argv)
{
	m_list = outlet_new(this->x_obj, 0);
}

pix_mean_color::~pix_mean_color()
{
	outlet_free(m_list);
}

void pix_mean_color::processYUVImage(imageStruct &image)
{
	t_atom out[4];
	unsigned int datasize = (image.xsize * image.ysize) >> 1;
	unsigned char *base = image.data;
	
	unsigned long sum[4] = {0,0,0,0};

	for (unsigned int i = 0; i != datasize; ++i)
	{
		for(int j = 0; j!= 4; ++j)
		{
			sum[j]  += base[j];
		}
		base += 4;
	}
        // use formulae from http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC29
        /*
         * [R]   [ 0.00456621  0.00000000  0.00625893 ]   [Y-16 ]
         * [G] = [ 0.00456621 -0.00153632 -0.00318811 ] * [U-128]
         * [B]   [ 0.00456621  0.00791071  0.00000000 ]   [V-128]
         */

        t_float y = (t_float)(sum[chY0]+sum[chY1]) / (t_float)(2*datasize) - 16;
        t_float u = (t_float) sum[chU] / (t_float)(datasize) - 128;
        t_float v = (t_float) sum[chV] / (t_float)(datasize) - 128;

        t_float r = FLOAT_CLAMP((t_float)(0.00456621*y             +0.00625893*v));
        t_float g = FLOAT_CLAMP((t_float)(0.00456621*y-0.00153632*u-0.00318811*v));
        t_float b = FLOAT_CLAMP((t_float)(0.00456621*y+0.00791071*u             ));

	SETFLOAT(out,   r);
	SETFLOAT(out+1, g);
        SETFLOAT(out+2, b);
	SETFLOAT(out+3, 1.0);

	outlet_list(m_list, 0, 4, out);	

}

void pix_mean_color::processGrayImage(imageStruct &image)
{
	t_atom out[4];
	unsigned int datasize = image.xsize * image.ysize;
	unsigned char *base = image.data;
	
	unsigned long sum = 0;

	for (unsigned int i = 0; i != datasize; ++i)
	{
		sum  += *base;
		base++;
	}

        t_float grey = (t_float)sum / (t_float)(datasize*255);
	
	SETFLOAT(out,   grey );
	SETFLOAT(out+1, grey);
	SETFLOAT(out+2, grey);
	SETFLOAT(out+3, 1.0);

	outlet_list(m_list, 0, 4, out);
}

void pix_mean_color::processRGBImage(imageStruct &image)
{
	t_atom out[4];
	unsigned int datasize = image.xsize * image.ysize;
	unsigned char *base = image.data;
	
	unsigned long sum[3] = {0,0,0};

	for (unsigned int i = 0; i != datasize; ++i)
	{
		for(int j = 0; j!= 3; ++j)
		{
			sum[j]  += base[j];
		}
		base += 3;
	}
	
	
	SETFLOAT(out,   (t_float)sum[0] / (t_float)(datasize*255));
        SETFLOAT(out+1, (t_float)sum[1] / (t_float)(datasize*255));
	SETFLOAT(out+2, (t_float)sum[2] / (t_float)(datasize*255));
	SETFLOAT(out+3, 1.0);

	outlet_list(m_list, 0, 4, out);
}

void pix_mean_color::processRGBAImage(imageStruct &image)
{
	unsigned int datasize = image.xsize * image.ysize;
	unsigned char *base = image.data;
	
	unsigned long sum[4] = {0,0,0,0};

	for (unsigned int i = 0; i != datasize; ++i)
	{
		for(int j = 0; j!= 4; ++j)
		{
			sum[j]  += base[j];
		}
		base += 4;
	}
	
	t_atom out[4];
	
	SETFLOAT(out,   (t_float)sum[chRed  ] / (t_float)(datasize*255) );
	SETFLOAT(out+1, (t_float)sum[chGreen] / (t_float)(datasize*255) );
	SETFLOAT(out+2, (t_float)sum[chBlue ] / (t_float)(datasize*255) );
	SETFLOAT(out+3, (t_float)sum[chAlpha] / (t_float)(datasize*255) );

	outlet_list(m_list, 0, 4, out);
}

void pix_mean_color :: obj_setupCallback(t_class *classPtr)
{

}