Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / plugins / threshold / histogramengine.C
blob20b28146341177a3f8e81761c42cb6aa51ec68f8
1 #include "histogramengine.h"
2 #include "../colors/plugincolors.h"
3 #include "vframe.h"
5 #include <stdio.h>
6 #include <string.h>
8 HistogramPackage::HistogramPackage()
9  : LoadPackage()
11         start = end = 0;
15 HistogramUnit::HistogramUnit(HistogramEngine *server)
16  : LoadClient(server)
18         for(int i = 0; i < 5; i++)
19                 accum[i] = new int64_t[HISTOGRAM_RANGE];
22 HistogramUnit::~HistogramUnit()
24         for(int i = 0; i < 5; i++)
25                 delete [] accum[i];
28 void HistogramUnit::process_package(LoadPackage *package)
30         HistogramPackage *pkg = (HistogramPackage*)package;
31         HistogramEngine *server = (HistogramEngine*)get_server();
34 #define HISTOGRAM_HEAD(type) \
35 { \
36         for(int i = pkg->start; i < pkg->end; i++) \
37         { \
38                 type *row = (type*)data->get_rows()[i]; \
39                 for(int j = 0; j < w; j++) \
40                 {
42 #define HISTOGRAM_TAIL(components) \
43                         v = (r * 76 + g * 150 + b * 29) >> 8; \
44 /*                      v = MAX(r, g); */ \
45 /*                      v = MAX(v, b); */ \
46                         r += -(int)(HISTOGRAM_MIN * 0xffff); \
47                         g += -(int)(HISTOGRAM_MIN * 0xffff); \
48                         b += -(int)(HISTOGRAM_MIN * 0xffff); \
49                         v += -(int)(HISTOGRAM_MIN * 0xffff); \
50                         CLAMP(r, 0, HISTOGRAM_RANGE); \
51                         CLAMP(g, 0, HISTOGRAM_RANGE); \
52                         CLAMP(b, 0, HISTOGRAM_RANGE); \
53                         CLAMP(v, 0, HISTOGRAM_RANGE); \
54                         accum_r[r]++; \
55                         accum_g[g]++; \
56                         accum_b[b]++; \
57                         accum_v[v]++; \
58 /*                      if(components == 4) accum_a[row[3]]++; */ \
59                         row += components; \
60                 } \
61         } \
66         VFrame *data = server->data;
68         int w = data->get_w();
69         int h = data->get_h();
70         int64_t *accum_r = accum[HISTOGRAM_RED];
71         int64_t *accum_g = accum[HISTOGRAM_GREEN];
72         int64_t *accum_b = accum[HISTOGRAM_BLUE];
73         int64_t *accum_a = accum[HISTOGRAM_ALPHA];
74         int64_t *accum_v = accum[HISTOGRAM_VALUE];
75         int r, g, b, a, y, u, v;
77         switch(data->get_color_model())
78         {
79                 case BC_RGB888:
80                         HISTOGRAM_HEAD(unsigned char)
81                         r = (row[0] << 8) | row[0];
82                         g = (row[1] << 8) | row[1];
83                         b = (row[2] << 8) | row[2];
84                         HISTOGRAM_TAIL(3)
85                         break;
86                 case BC_RGB_FLOAT:
87                         HISTOGRAM_HEAD(float)
88                         r = (int)(row[0] * 0xffff);
89                         g = (int)(row[1] * 0xffff);
90                         b = (int)(row[2] * 0xffff);
91                         HISTOGRAM_TAIL(3)
92                         break;
93                 case BC_YUV888:
94                         HISTOGRAM_HEAD(unsigned char)
95                         y = (row[0] << 8) | row[0];
96                         u = (row[1] << 8) | row[1];
97                         v = (row[2] << 8) | row[2];
98                         server->yuv->yuv_to_rgb_16(r, g, b, y, u, v);
99                         HISTOGRAM_TAIL(3)
100                         break;
101                 case BC_RGBA8888:
102                         HISTOGRAM_HEAD(unsigned char)
103                         r = (row[0] << 8) | row[0];
104                         g = (row[1] << 8) | row[1];
105                         b = (row[2] << 8) | row[2];
106                         HISTOGRAM_TAIL(4)
107                         break;
108                 case BC_RGBA_FLOAT:
109                         HISTOGRAM_HEAD(float)
110                         r = (int)(row[0] * 0xffff);
111                         g = (int)(row[1] * 0xffff);
112                         b = (int)(row[2] * 0xffff);
113                         HISTOGRAM_TAIL(4)
114                         break;
115                 case BC_YUVA8888:
116                         HISTOGRAM_HEAD(unsigned char)
117                         y = (row[0] << 8) | row[0];
118                         u = (row[1] << 8) | row[1];
119                         v = (row[2] << 8) | row[2];
120                         server->yuv->yuv_to_rgb_16(r, g, b, y, u, v);
121                         HISTOGRAM_TAIL(4)
122                         break;
123                 case BC_RGB161616:
124                         HISTOGRAM_HEAD(uint16_t)
125                         r = row[0];
126                         g = row[1];
127                         b = row[2];
128                         HISTOGRAM_TAIL(3)
129                         break;
130                 case BC_YUV161616:
131                         HISTOGRAM_HEAD(uint16_t)
132                         y = row[0];
133                         u = row[1];
134                         v = row[2];
135                         server->yuv->yuv_to_rgb_16(r, g, b, y, u, v);
136                         HISTOGRAM_TAIL(3)
137                         break;
138                 case BC_RGBA16161616:
139                         HISTOGRAM_HEAD(uint16_t)
140                         r = row[0];
141                         g = row[1];
142                         b = row[2];
143                         HISTOGRAM_TAIL(3);
144                         break;
145                 case BC_YUVA16161616:
146                         HISTOGRAM_HEAD(uint16_t)
147                         y = row[0];
148                         u = row[1];
149                         v = row[2];
150                         server->yuv->yuv_to_rgb_16(r, g, b, y, u, v);
151                         HISTOGRAM_TAIL(4)
152                         break;
153         }
158         
163 HistogramEngine::HistogramEngine(int total_clients, int total_packages)
164  : LoadServer(total_clients, total_packages)
166         yuv = new YUV;
167         data = 0;
168         for(int i = 0; i < 5; i++)
169                 accum[i] = new int64_t[HISTOGRAM_RANGE];
172 HistogramEngine::~HistogramEngine()
174         delete yuv;
175         for(int i = 0; i < 5; i++)
176                 delete [] accum[i];
179 void HistogramEngine::process_packages(VFrame *data)
181         this->data = data;
182         LoadServer::process_packages();
183         for(int i = 0; i < 5; i++)
184         {
185                 bzero(accum[i], sizeof(int64_t) * HISTOGRAM_RANGE);
186         }
188         for(int i = 0; i < get_total_clients(); i++)
189         {
190                 HistogramUnit *unit = (HistogramUnit*)get_client(i);
191                 for(int k = 0; k < 5; k++)
192                 {
193                         for(int j = 0; j < HISTOGRAM_RANGE; j++)
194                         {
195                                 accum[k][j] += unit->accum[k][j];
196                         }
197                 }
198         }
202 void HistogramEngine::init_packages()
204         for(int i = 0; i < get_total_packages(); i++)
205         {
206                 HistogramPackage *package = (HistogramPackage*)get_package(i);
207                 package->start = data->get_h() * i / get_total_packages();
208                 package->end = data->get_h() * (i + 1) / get_total_packages();
209         }
211 // Initialize clients here in case some don't get run.
212         for(int i = 0; i < get_total_clients(); i++)
213         {
214                 HistogramUnit *unit = (HistogramUnit*)get_client(i);
215                 for(int i = 0; i < 5; i++)
216                         bzero(unit->accum[i], sizeof(int64_t) * HISTOGRAM_RANGE);
217         }
220 LoadClient* HistogramEngine::new_client()
222         return (LoadClient*)new HistogramUnit(this);
225 LoadPackage* HistogramEngine::new_package()
227         return (LoadPackage*)new HistogramPackage;