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
|
/*
* Copyright (C) 2007 - 2008 Lars Kuhtz, ExactCODE GmbH Germany.
* Copyright (C) 2009 - 2010 René Rebe, ExactCODE GmbH Germany.
*
* 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; version 2. A copy of the GNU General
* Public License can be found in the file LICENSE.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
* ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* Alternatively, commercial licensing options are available from the
* copyright holder ExactCODE GmbH Germany.
*/
#ifndef _PIXEL_ITERATOR_HH_
#define _PIXEL_ITERATOR_HH_
#include "Image.hh"
#include <iterator>
#include <vector>
#ifdef __APPLE__
#include <sys/types.h>
#endif
namespace BarDecode
{
typedef int pos_t;
typedef int threshold_t;
template<bool vertical = false>
class PixelIterator :
public std::iterator<std::output_iterator_tag,
bool,
std::ptrdiff_t>
{
protected:
typedef PixelIterator self_t;
public:
typedef bool value_type;
PixelIterator(const Image* img, int concurrent_lines = 4, int line_skip = 8, threshold_t threshold = 0) :
img(img),
it_size(concurrent_lines),
line_skip(line_skip),
img_it(it_size),
threshold(threshold),
x(0),
y(0),
lum(0),
valid_cache(false)
{
// FIXME insert an optimized code path for img->h <= it_size
for (int i = 0; i < it_size; ++i) {
img_it[i] = img->begin().at(0,std::min((int)i,img->h-1));
*img_it[i];
}
}
virtual ~PixelIterator() {};
self_t& operator++()
{
valid_cache = false;
if ( x < img->w-1 ) {
++x;
for (int i = 0; i < it_size; ++i) {
++img_it[i];
*img_it[i];
}
} else {
x = 0;
int todo = (img->h-1) - y;
if ( todo > line_skip + (it_size-1) ) {
y += line_skip;
for (int i = 0; i < it_size; ++i) {
img_it[i] = img_it[i].at(x,y+(int)i);
*img_it[i];
}
} else if ( todo <= line_skip ) {
// we are at the end
//++img_it[it_size-1];
img_it[it_size-1] = img->end();
} else {
y += line_skip;
for (int i = 0; i < it_size; ++i) {
img_it[i] = img_it[i].at(x,std::min(y+(int)i,img->h-1));
*img_it[i];
}
}
}
return *this;
};
// FIXME it seems that the median (or something similar) is the better choice.
const value_type operator*() const
{
if (valid_cache) return cache;
double tmp=0;
//uint16_t min = 255;
//uint16_t max = 0;
for (int i = 0; i < it_size; ++i) {
//min = std::min(min,img_it[i].getL());
//max = std::max(max,img_it[i].getL());
tmp += img_it[i].getL();
}
//lum = (tmp - (double) (min+max)) / (double) (it_size-2);
lum = tmp / it_size;
cache = lum < threshold;
valid_cache = true;
return cache;
}
//value_type* operator->();
//const value_type* operator->() const;
self_t at(pos_t x, pos_t y) const
{
// FIXME insert an optimized code path for img->h >= y+it_size
self_t tmp = *this;
for (int i = 0; i < it_size; ++i) {
tmp.img_it[i] = tmp.img_it[i].at(x,std::min(y+(int)i,img->h));
}
tmp.valid_cache = false;
tmp.x = x;
tmp.y = y;
return tmp;
}
pos_t get_x() const { return x; }
pos_t get_y() const { return y; }
threshold_t get_threshold() const { return threshold; }
void set_threshold(threshold_t new_threshold)
{
valid_cache = false;
threshold = new_threshold;
}
bool end() const { return !(img_it[it_size-1] != img->end()); }
double get_lum() const
{
if (! valid_cache) {
operator*();
}
return lum;
}
long get_x_size() const { return img->w; }
long get_y_size() const { return img->h; }
long get_line_length() const { return get_x_size(); }
protected:
const Image* img;
int it_size;
int line_skip;
std::vector<Image::const_iterator> img_it;
threshold_t threshold;
pos_t x;
pos_t y;
mutable double lum;
mutable bool cache;
mutable bool valid_cache;
}; // class PixelIterator<vertical = true>
// vertical iteration
template<>
class PixelIterator<true> :
public std::iterator<std::output_iterator_tag,
bool,
std::ptrdiff_t>
{
protected:
typedef PixelIterator self_t;
public:
typedef bool value_type;
PixelIterator(const Image* img, int concurrent_lines = 4, int line_skip = 8, threshold_t threshold = 0) :
img(img),
it_size(concurrent_lines),
line_skip(line_skip),
img_it(it_size),
threshold(threshold),
x(0),
y(0),
lum(0),
valid_cache(false)
{
// FIXME insert an optimized code path for img->h <= it_size
for (int i = 0; i < it_size; ++i) {
img_it[i] = img->begin().at(std::min((int)i,img->w-1),0);
*img_it[i];
}
}
virtual ~PixelIterator() {};
self_t& operator++()
{
valid_cache = false;
if ( y < img->h-1 ) {
++y;
for (int i = 0; i < it_size; ++i) {
img_it[i].down();
*img_it[i];
}
} else {
y = 0;
int todo = (img->w-1) - x;
if ( todo > line_skip + (it_size-1) ) {
x += line_skip;
for (int i = 0; i < it_size; ++i) {
img_it[i] = img_it[i].at(x+(int) i,y);
*img_it[i];
}
} else if ( todo <= line_skip ) {
img_it[it_size-1] = img->end();
} else {
x += line_skip;
for (int i = 0; i < it_size; ++i) {
img_it[i] = img_it[i].at(std::min(x+(int)i,img->w-1),y);
*img_it[i];
}
}
}
return *this;
};
// FIXME it seems that the median (or something similar) is the better choice.
const value_type operator*() const
{
if (valid_cache) return cache;
double tmp=0;
//uint16_t min = 255;
//uint16_t max = 0;
for (int i = 0; i < it_size; ++i) {
//min = std::min(min,img_it[i].getL());
//max = std::max(max,img_it[i].getL());
tmp += img_it[i].getL();
}
//lum = (tmp - (double) (min+max)) / (double) (it_size-2);
lum = tmp / it_size;
cache = lum < threshold;
valid_cache = true;
return cache;
}
//value_type* operator->();
//const value_type* operator->() const;
self_t at(pos_t x, pos_t y) const
{
// FIXME insert an optimized code path for img->h >= y+it_size
self_t tmp = *this;
for (int i = 0; i < it_size; ++i) {
tmp.img_it[i] = tmp.img_it[i].at(std::min(x+(int)i,img->w-1),y);
}
tmp.valid_cache = false;
tmp.x = x;
tmp.y = y;
return tmp;
}
pos_t get_x() const { return x; }
pos_t get_y() const { return y; }
threshold_t get_threshold() const { return threshold; }
void set_threshold(threshold_t new_threshold)
{
valid_cache = false;
threshold = new_threshold;
}
bool end() const { return !(img_it[it_size-1] != img->end()); }
double get_lum() const
{
if (! valid_cache) {
operator*();
}
return lum;
}
long get_x_size() const { return img->w; }
long get_y_size() const { return img->h; }
long get_line_length() const { return get_y_size(); }
protected:
const Image* img;
int it_size;
int line_skip;
std::vector<Image::const_iterator> img_it;
threshold_t threshold;
pos_t x;
pos_t y;
mutable double lum;
mutable bool cache;
mutable bool valid_cache;
}; // class PixelIterator<vertical = true>
}; // namespace BarDecode
#endif // _PIXEL_ITERATOR_HH_
|