4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "histogramengine.h"
29 HistogramPackage::HistogramPackage()
36 HistogramUnit::HistogramUnit(HistogramEngine *server)
39 for(int i = 0; i < 5; i++)
40 accum[i] = new int64_t[HISTOGRAM_RANGE];
43 HistogramUnit::~HistogramUnit()
45 for(int i = 0; i < 5; i++)
49 void HistogramUnit::process_package(LoadPackage *package)
51 HistogramPackage *pkg = (HistogramPackage*)package;
52 HistogramEngine *server = (HistogramEngine*)get_server();
55 #define HISTOGRAM_HEAD(type) \
57 for(int i = pkg->start; i < pkg->end; i++) \
59 type *row = (type*)data->get_rows()[i]; \
60 for(int j = 0; j < w; j++) \
63 #define HISTOGRAM_TAIL(components) \
64 v = (r * 76 + g * 150 + b * 29) >> 8; \
65 /* v = MAX(r, g); */ \
66 /* v = MAX(v, b); */ \
67 r += -(int)(HISTOGRAM_MIN * 0xffff); \
68 g += -(int)(HISTOGRAM_MIN * 0xffff); \
69 b += -(int)(HISTOGRAM_MIN * 0xffff); \
70 v += -(int)(HISTOGRAM_MIN * 0xffff); \
71 CLAMP(r, 0, HISTOGRAM_RANGE); \
72 CLAMP(g, 0, HISTOGRAM_RANGE); \
73 CLAMP(b, 0, HISTOGRAM_RANGE); \
74 CLAMP(v, 0, HISTOGRAM_RANGE); \
79 /* if(components == 4) accum_a[row[3]]++; */ \
87 VFrame *data = server->data;
89 int w = data->get_w();
90 //int h = data->get_h();
91 int64_t *accum_r = accum[HISTOGRAM_RED];
92 int64_t *accum_g = accum[HISTOGRAM_GREEN];
93 int64_t *accum_b = accum[HISTOGRAM_BLUE];
94 //int64_t *accum_a = accum[HISTOGRAM_ALPHA];
95 int64_t *accum_v = accum[HISTOGRAM_VALUE];
96 int r, g, b, y, u, v; // ,a;
98 switch(data->get_color_model())
101 HISTOGRAM_HEAD(unsigned char)
102 r = (row[0] << 8) | row[0];
103 g = (row[1] << 8) | row[1];
104 b = (row[2] << 8) | row[2];
108 HISTOGRAM_HEAD(float)
109 r = (int)(row[0] * 0xffff);
110 g = (int)(row[1] * 0xffff);
111 b = (int)(row[2] * 0xffff);
115 HISTOGRAM_HEAD(unsigned char)
116 y = (row[0] << 8) | row[0];
117 u = (row[1] << 8) | row[1];
118 v = (row[2] << 8) | row[2];
119 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
123 HISTOGRAM_HEAD(unsigned char)
124 r = (row[0] << 8) | row[0];
125 g = (row[1] << 8) | row[1];
126 b = (row[2] << 8) | row[2];
130 HISTOGRAM_HEAD(float)
131 r = (int)(row[0] * 0xffff);
132 g = (int)(row[1] * 0xffff);
133 b = (int)(row[2] * 0xffff);
137 HISTOGRAM_HEAD(unsigned char)
138 y = (row[0] << 8) | row[0];
139 u = (row[1] << 8) | row[1];
140 v = (row[2] << 8) | row[2];
141 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
145 HISTOGRAM_HEAD(uint16_t)
152 HISTOGRAM_HEAD(uint16_t)
156 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
159 case BC_RGBA16161616:
160 HISTOGRAM_HEAD(uint16_t)
166 case BC_YUVA16161616:
167 HISTOGRAM_HEAD(uint16_t)
171 YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v);
184 HistogramEngine::HistogramEngine(int total_clients, int total_packages)
185 : LoadServer(total_clients, total_packages)
188 for(int i = 0; i < 5; i++)
189 accum[i] = new int64_t[HISTOGRAM_RANGE];
192 HistogramEngine::~HistogramEngine()
194 for(int i = 0; i < 5; i++)
198 void HistogramEngine::process_packages(VFrame *data)
201 LoadServer::process_packages();
202 for(int i = 0; i < 5; i++)
204 bzero(accum[i], sizeof(int64_t) * HISTOGRAM_RANGE);
207 for(int i = 0; i < get_total_clients(); i++)
209 HistogramUnit *unit = (HistogramUnit*)get_client(i);
210 for(int k = 0; k < 5; k++)
212 for(int j = 0; j < HISTOGRAM_RANGE; j++)
214 accum[k][j] += unit->accum[k][j];
221 void HistogramEngine::init_packages()
223 for(int i = 0; i < get_total_packages(); i++)
225 HistogramPackage *package = (HistogramPackage*)get_package(i);
226 package->start = data->get_h() * i / get_total_packages();
227 package->end = data->get_h() * (i + 1) / get_total_packages();
230 // Initialize clients here in case some don't get run.
231 for(int i = 0; i < get_total_clients(); i++)
233 HistogramUnit *unit = (HistogramUnit*)get_client(i);
234 for(int i = 0; i < 5; i++)
235 bzero(unit->accum[i], sizeof(int64_t) * HISTOGRAM_RANGE);
239 LoadClient* HistogramEngine::new_client()
241 return (LoadClient*)new HistogramUnit(this);
244 LoadPackage* HistogramEngine::new_package()
246 return (LoadPackage*)new HistogramPackage;