17 const std::string & _identifier) :
19 s_identifier(_identifier),
20 destination(_destination),
21 hdrText(
SSTR(
"ROCS\x01", _identifier,
".hist")),
22 hdrBluenessText(
SSTR(
"ROCS\x01", _identifier,
".blueness")){
24 if (s_identifier.compare(
"vnir") == 0) {
26 blHigh.calData = caltables::vnirHighCal;
27 blHigh.roi = {0, 290, caltables::vnirHighCal_spatial_pixels, caltables::vnirHighCal_spectral_pixels};
29 blHigh.acc =
new double[caltables::vnirHighCal_spatial_pixels];
30 blLow.calData = caltables::vnirLowCal;
31 blLow.roi = {0, 95, caltables::vnirLowCal_spatial_pixels, caltables::vnirLowCal_spectral_pixels};
33 blLow.acc =
new double[caltables::vnirHighCal_spatial_pixels];
35 }
else if (s_identifier.compare(
"swir") == 0) {
42 bitShift = sensorBits - histBits;
43 histCount = 1 << histBits;
44 bitMask = histCount - 1;
68 void StatsExtractor::extractSetup() {
70 destinationSocket =
new zmq::socket_t(
ctx, ZMQ_PUB);
71 destinationSocket->connect(destination.
endpoint.c_str());
73 hist =
new uint32_t[histCount]();
76 void StatsExtractor::extractImpl(
unsigned int _width,
78 unsigned int _bytesPerPixel,
81 zmq::message_t * sourceImage) {
86 min=UINT_MAX, max=0, avg=0;
87 memset(hist, 0, histCount*
sizeof(uint32_t));
89 uint16_t *data = (uint16_t *)sourceImage->data();
90 uint16_t *end = data + sourceImage->size()/
sizeof(uint16_t);
91 for(; data != end; ++data) {
94 }
else if((*data)>max) {
98 ++(hist[((*data)>>bitShift)&bitMask]);
100 avg /= (
unsigned int)(sourceImage->size()/
sizeof(uint16_t));
104 data = (uint16_t *)sourceImage->data();
105 switch(_bytesPerPixel) {
107 blHigh.accumulate<uint8_t>((uint8_t *)sourceImage->data(), _width, _height);
108 blLow.accumulate<uint8_t>((uint8_t *)sourceImage->data(), _width, _height);
111 blHigh.accumulate<uint16_t>((uint16_t *)sourceImage->data(), _width, _height);
112 blLow.accumulate<uint16_t>((uint16_t *)sourceImage->data(), _width, _height);
115 blHigh.accumulate<uint32_t>((uint32_t *)sourceImage->data(), _width, _height);
116 blLow.accumulate<uint32_t>((uint32_t *)sourceImage->data(), _width, _height);
121 for(
unsigned int j = 0; j < blHigh.roi.width; ++j) {
122 blueness += blHigh.acc[j] * blHigh.acc[j] / blLow.acc[j];
124 blueness /= blHigh.roi.width;
132 sendBlueness(blueness);
136 void StatsExtractor::extractTeardown() {
138 delete destinationSocket;
142 void StatsExtractor::sendStatus() {
143 zmq::message_t msgHdr(11+s_identifier.length());
144 char* statusHeaderText = (
char*)malloc(12+s_identifier.length());
145 snprintf(statusHeaderText, 12+s_identifier.length(),
"ROCS\x01%s.stats", s_identifier.c_str());
146 memcpy((
char*)msgHdr.data(), statusHeaderText, 11+s_identifier.length());
147 destinationSocket->send(msgHdr, ZMQ_SNDMORE);
148 free(statusHeaderText);
150 Json::FastWriter writer;
155 std::string data = writer.write(root);
156 zmq::message_t msgData(data.length());
157 memcpy((
char *)msgData.data(), data.c_str(), data.length());
158 destinationSocket->send(msgData);
161 void StatsExtractor::sendHist() {
162 zmq::message_t msgHdr(hdrText.length());
163 memcpy((
char*)msgHdr.data(), hdrText.c_str(), hdrText.length());
164 destinationSocket->send(msgHdr, ZMQ_SNDMORE);
166 Json::FastWriter writer;
168 for(uint32_t i=0; i<histCount; i++) {
169 root.append(hist[i]);
171 std::string data = writer.write(root);
172 zmq::message_t msgData(data.length());
173 memcpy((
char *)msgData.data(), data.c_str(), data.length());
174 destinationSocket->send(msgData);
177 void StatsExtractor::sendBlueness(
double blueness) {
178 zmq::message_t msgHdr(hdrBluenessText.length());
179 memcpy((
char*)msgHdr.data(), hdrBluenessText.c_str(), hdrBluenessText.length());
180 destinationSocket->send(msgHdr, ZMQ_SNDMORE);
182 Json::FastWriter writer;
184 if(blueness != blueness) {
185 root = Json::Value::null;
189 std::string data = writer.write(root);
190 zmq::message_t msgData(data.length());
191 memcpy((
char *)msgData.data(), data.c_str(), data.length());
192 destinationSocket->send(msgData);
196 template <
typename T>
void StatsExtractor::CalAvgData::accumulate(T* data,
unsigned int w,
unsigned int h) {
199 for(
unsigned int j = 0; j < roi.width; ++j) {
202 for(
unsigned int i = 0; i < roi.height; ++i) {
203 for(
unsigned int j = 0; j < roi.width; ++j) {
204 temp = data[(i + roi.y) * w + j + roi.x] - dark;
208 acc[j] += temp * calData[i * roi.width + j];
std::string SSTR(Args &&...components)
Creates a temporary string stream for string concatenation.