3 #include <FL/Fl_Shared_Image.H> 23 for (
int i = 0; i < *
argc; i++)
29 if (!setlocale(LC_ALL,
"POSIX"))
30 PRINT_WARN(
"Failed to setlocale(); config file may not be parse correctly\n");
72 static inline bool pixel_is_set(uint8_t *pixels,
const unsigned int width,
const unsigned int depth,
73 const unsigned int x,
const unsigned int y, uint8_t threshold)
75 return ((pixels + (y * width * depth) + x * depth)[0] > threshold);
87 std::vector<std::vector<point_t> > &polys)
90 const int threshold = 127;
92 Fl_Shared_Image *img = Fl_Shared_Image::get(filename.c_str());
94 std::cerr <<
"failed to open file: " << filename << std::endl;
103 const unsigned int width = img->w();
104 const unsigned height = img->h();
105 const unsigned int depth = img->d();
106 uint8_t *pixels = (uint8_t *)img->data()[0];
110 std::set<std::vector<uint32_t> > edges;
112 for (
unsigned int y = 0; y < height; y++) {
113 for (
unsigned int x = 0; x < width; x++) {
115 if (
pixel_is_set(pixels, width, depth, x, y, threshold))
119 std::vector<uint32_t> edge[4];
121 for (
int i = 0; i < 4; i++)
145 for (
int i = 0; i < 4; i++) {
147 std::vector<uint32_t> inv(4);
153 std::set<std::vector<uint32_t> >::iterator it = edges.find(inv);
155 if (it == edges.end())
156 edges.insert(edge[i]);
163 std::multimap<point_t, point_t> mmap;
167 std::pair<point_t, point_t> p(
point_t((*it)[0], (*it)[1]),
point_t((*it)[2], (*it)[3]));
171 for (std::multimap<point_t, point_t>::iterator seedit = mmap.begin(); seedit != mmap.end();
172 seedit = mmap.begin()) {
173 std::vector<point_t> poly;
175 while (seedit != mmap.end()) {
181 size_t psize = poly.size();
185 double ldx =
direction(poly[psize - 1].x - poly[psize - 2].x);
186 double ldy =
direction(poly[psize - 1].y - poly[psize - 2].y);
190 double ndx =
direction(pt.
x - poly[psize - 1].x);
191 double ndy =
direction(pt.
y - poly[psize - 1].y);
196 if (ldx == ndx && ldy == ndy)
197 poly[psize - 1] = pt;
206 seedit = mmap.find(next);
209 polys.push_back(poly);
The Stage library uses its own namespace.
static std::vector< std::string > args
static const Color magenta
void Init(int *argc, char **argv[])
double direction(double a)
static bool pixel_is_set(uint8_t *pixels, const unsigned int width, const unsigned int depth, const unsigned int x, const unsigned int y, uint8_t threshold)
int polys_from_image_file(const std::string &filename, std::vector< std::vector< point_t > > &polys)
rotated rectangle
double constrain(double val, double minval, double maxval)
return val, or minval if val < minval, or maxval if val > maxval
static const Color yellow
point_t * unit_square_points_create()