2014-06-18 08:47:10 +02:00
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2014-06-22 15:50:42 +02:00
|
|
|
#include <sstream>
|
2014-06-18 08:47:10 +02:00
|
|
|
#include <cmath>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2014-06-24 19:14:59 +02:00
|
|
|
#include <chrono>
|
2014-06-18 08:47:10 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
|
2014-06-18 08:47:10 +02:00
|
|
|
template <typename T>
|
|
|
|
struct TPoint {
|
|
|
|
T x;
|
|
|
|
T y;
|
|
|
|
|
|
|
|
TPoint(T a, T b)
|
|
|
|
: x(a)
|
|
|
|
, y(b)
|
|
|
|
{}
|
|
|
|
};
|
2014-06-26 19:18:32 +02:00
|
|
|
typedef TPoint<int> Point;
|
2014-06-18 08:47:10 +02:00
|
|
|
typedef TPoint<int> APoint; // absolute point, can be negative
|
|
|
|
typedef TPoint<double> DPoint; // absolute point, can be negative
|
|
|
|
template<typename Elem, typename Traits, typename T>
|
|
|
|
std::basic_ostream<Elem, Traits>& operator << (std::basic_ostream<Elem, Traits>& o, TPoint<T> const& p)
|
|
|
|
{
|
|
|
|
o << "(" << p.x << ", " << p.y << ")";
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Image {
|
|
|
|
unsigned int width;
|
|
|
|
unsigned int height;
|
|
|
|
uint8_t* r_chan;
|
|
|
|
uint8_t* g_chan;
|
|
|
|
uint8_t* b_chan;
|
|
|
|
|
|
|
|
Image()
|
|
|
|
: width(0)
|
|
|
|
, height(0)
|
|
|
|
, r_chan(NULL)
|
|
|
|
, g_chan(NULL)
|
|
|
|
, b_chan(NULL)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Image(unsigned int w, unsigned int h)
|
|
|
|
{
|
|
|
|
this->width = w;
|
|
|
|
this->height = h;
|
|
|
|
r_chan = new uint8_t[width * height];
|
2014-06-26 19:18:32 +02:00
|
|
|
memset(r_chan, 255, width * height * sizeof (uint8_t));
|
2014-06-18 08:47:10 +02:00
|
|
|
g_chan = new uint8_t[width * height];
|
|
|
|
memset(g_chan, 0, width * height * sizeof (uint8_t));
|
|
|
|
b_chan = new uint8_t[width * height];
|
|
|
|
memset(b_chan, 0, width * height * sizeof (uint8_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
Image(string const& path)
|
|
|
|
: Image()
|
|
|
|
{
|
|
|
|
ifstream is(path);
|
|
|
|
if (!is.is_open())
|
|
|
|
{
|
|
|
|
cerr << "Cannot open file '" << path << "'" << endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->read_header(is))
|
|
|
|
{
|
|
|
|
cerr << "Invalid header." << endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->read_body(is))
|
|
|
|
{
|
|
|
|
delete r_chan;
|
|
|
|
r_chan = nullptr;
|
|
|
|
delete g_chan;
|
|
|
|
r_chan = nullptr;
|
|
|
|
delete b_chan;
|
|
|
|
r_chan = nullptr;
|
|
|
|
|
|
|
|
cerr << "Invalid header." << endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool save(string const& path)
|
|
|
|
{
|
|
|
|
ofstream os(path);
|
|
|
|
if (!os.is_open())
|
|
|
|
{
|
|
|
|
cerr << "Cannot open file '" << path << "'" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this->write_header(os);
|
|
|
|
this->write_body(os);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_pixel(unsigned int x, unsigned int y, uint8_t r, uint8_t g, uint8_t b)
|
|
|
|
{
|
2014-06-22 15:50:42 +02:00
|
|
|
if (x >= width || y >= height)
|
|
|
|
{
|
2014-06-26 23:45:02 +02:00
|
|
|
cerr << __LINE__ << " | Point (" << x << ", " << y << ") out of bounds" << endl;
|
2014-06-22 15:50:42 +02:00
|
|
|
cerr << " Image dimensions: " << width << " x " << height << endl;
|
2014-06-26 23:45:02 +02:00
|
|
|
// assert(false);
|
|
|
|
return;
|
2014-06-22 15:50:42 +02:00
|
|
|
}
|
2014-06-18 08:47:10 +02:00
|
|
|
int const index = y * width + x;
|
|
|
|
r_chan[index] = r;
|
|
|
|
g_chan[index] = g;
|
|
|
|
b_chan[index] = b;
|
|
|
|
}
|
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
void set_pixel(Point const& p, uint8_t r, uint8_t g, uint8_t b)
|
|
|
|
{
|
|
|
|
this->set_pixel(p.x, p.y, r, g, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_pixel(APoint const& p, uint8_t& r, uint8_t& g, uint8_t& b) const
|
|
|
|
{
|
|
|
|
if (p.x < 0 || p.x >= (int) width || p.y < 0 || p.y >= (int) height)
|
|
|
|
{
|
|
|
|
// set out of domain pixels to black
|
|
|
|
r = 0;
|
|
|
|
g = 0;
|
|
|
|
b = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int const index = p.y * width + p.x;
|
|
|
|
r = r_chan[index];
|
|
|
|
g = g_chan[index];
|
|
|
|
b = b_chan[index];
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:47:10 +02:00
|
|
|
private:
|
|
|
|
bool read_header(std::ifstream& istr)
|
|
|
|
{
|
|
|
|
// check magic
|
|
|
|
if (istr.get() != 'P' )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char type = static_cast<char>(istr.get());
|
|
|
|
if (type != '6')
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (istr.get() != '\n')
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip comments
|
|
|
|
while (istr.peek() == '#')
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
std::getline(istr, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get size
|
|
|
|
istr >> width >> height;
|
|
|
|
if (width == 0 || height == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get maxvalue
|
|
|
|
if (istr.get() != '\n')
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int max_value = -1;
|
|
|
|
istr >> max_value;
|
|
|
|
if (max_value > 255)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (istr.get() != '\n')
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
// cout << "width: " << width << endl;
|
|
|
|
// cout << "height: " << height << endl;
|
2014-06-18 08:47:10 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_header(std::ofstream& ostr)
|
|
|
|
{
|
|
|
|
ostr << "P6" << endl;
|
|
|
|
ostr << width << " " << height << endl;
|
|
|
|
ostr << "255" << endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool read_body(std::ifstream& istr)
|
|
|
|
{
|
|
|
|
r_chan = new uint8_t[width * height];
|
|
|
|
g_chan = new uint8_t[width * height];
|
|
|
|
b_chan = new uint8_t[width * height];
|
|
|
|
|
|
|
|
for (unsigned int row = 0; row < height; ++row)
|
|
|
|
{
|
|
|
|
for (unsigned int col = 0; col < width; ++col)
|
|
|
|
{
|
|
|
|
int index = row * width + col;
|
|
|
|
r_chan[index] = istr.get();
|
|
|
|
g_chan[index] = istr.get();
|
|
|
|
b_chan[index] = istr.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_body(std::ofstream& ostr)
|
|
|
|
{
|
|
|
|
for (unsigned int row = 0; row < height; ++row)
|
|
|
|
{
|
|
|
|
for (unsigned int col = 0; col < width; ++col)
|
|
|
|
{
|
|
|
|
int index = row * width + col;
|
|
|
|
ostr << (char) r_chan[index];
|
|
|
|
ostr << (char) g_chan[index];
|
|
|
|
ostr << (char) b_chan[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
|
2014-06-18 08:47:10 +02:00
|
|
|
//
|
|
|
|
//
|
2014-06-22 15:50:42 +02:00
|
|
|
// Trigonometry
|
2014-06-18 08:47:10 +02:00
|
|
|
//
|
|
|
|
|
2014-06-26 19:18:32 +02:00
|
|
|
DPoint convert_grid_coord(Image const& img, APoint const& p)
|
2014-06-18 08:47:10 +02:00
|
|
|
{
|
2014-06-22 15:50:42 +02:00
|
|
|
return DPoint(p.x - img.width / 2.0f + 0.5, p.y - img.height / 2.0f + 0.5);
|
2014-06-18 08:47:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-26 19:18:32 +02:00
|
|
|
double convert_radian(Image const& img, APoint const& p, double const ratio)
|
2014-06-18 08:47:10 +02:00
|
|
|
{
|
2014-06-22 15:50:42 +02:00
|
|
|
DPoint centered = convert_grid_coord(img, p);
|
|
|
|
double const cos_value = centered.x * ratio;
|
|
|
|
double const sin_value = - (centered.y * ratio);
|
2014-06-18 08:47:10 +02:00
|
|
|
double angle = acos(cos_value);
|
|
|
|
if (sin_value < 0)
|
2014-06-24 00:59:04 +02:00
|
|
|
{
|
|
|
|
angle = (2 * M_PI) - angle;
|
|
|
|
}
|
2014-06-18 08:47:10 +02:00
|
|
|
|
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
DPoint convert_abs_coord(double const angle, double const ratio)
|
2014-06-18 08:47:10 +02:00
|
|
|
{
|
2014-06-24 19:14:59 +02:00
|
|
|
return DPoint(cos(angle) / ratio, - sin(angle) / ratio);
|
2014-06-18 08:47:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
APoint convert_img_coord(Image const& img, DPoint const& p)
|
2014-06-18 08:47:10 +02:00
|
|
|
{
|
2014-06-22 15:50:42 +02:00
|
|
|
int x = round(p.x + (img.width / 2.0f) - 0.5);
|
|
|
|
int y = round(p.y + (img.height / 2.0f) - 0.5);
|
2014-06-24 19:14:59 +02:00
|
|
|
return APoint(x, y);
|
|
|
|
}
|
2014-06-22 15:50:42 +02:00
|
|
|
|
2014-06-26 19:18:32 +02:00
|
|
|
DPoint convert_img_coord_precision(Image const& img, DPoint const& p)
|
|
|
|
{
|
|
|
|
int x = p.x + (img.width / 2.0f) - 0.5;
|
|
|
|
int y = p.y + (img.height / 2.0f) - 0.5;
|
|
|
|
return DPoint(x, y);
|
|
|
|
}
|
|
|
|
|
2014-06-24 19:14:59 +02:00
|
|
|
void convert_abs_to_polar_coord(DPoint const& p, double& angle, double& dist)
|
|
|
|
{
|
2014-06-26 19:18:32 +02:00
|
|
|
angle = atan2(-p.y, p.x);
|
2014-06-24 19:14:59 +02:00
|
|
|
dist = sqrt(p.x * p.x + p.y * p.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
DPoint convert_polar_to_grid_coord(double const angle, double const distance)
|
|
|
|
{
|
2014-06-26 19:18:32 +02:00
|
|
|
return DPoint(cos(angle) * distance, - (sin(angle) * distance));
|
2014-06-18 08:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double compute_ratio(Image const& img)
|
|
|
|
{
|
2014-06-24 00:59:04 +02:00
|
|
|
double const trigo_length = (sqrt(img.width * img.width + img.height * img.height) - 1) / 2;
|
2014-06-18 08:47:10 +02:00
|
|
|
return 1.0f / trigo_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
bool fequal(float a, float b, float sigma)
|
|
|
|
{
|
|
|
|
return abs(a - b) < sigma;
|
|
|
|
}
|
|
|
|
|
|
|
|
void compute_output_size(Image const& src, double const rotation, unsigned int& width, unsigned int& height)
|
|
|
|
{
|
|
|
|
double const ratio = compute_ratio(src);
|
2014-06-22 15:50:42 +02:00
|
|
|
double min_w = 0;
|
|
|
|
double max_w = 0;
|
|
|
|
double min_h = 0;
|
|
|
|
double max_h = 0;
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-06-19 00:05:49 +02:00
|
|
|
//cout << "Image dimensions: " << src.width << " x " << src.height << endl;
|
2014-06-26 19:18:32 +02:00
|
|
|
APoint p(0, 0);
|
2014-06-18 08:47:10 +02:00
|
|
|
double angle = convert_radian(src, p, ratio);
|
2014-06-22 15:50:42 +02:00
|
|
|
DPoint tl = convert_abs_coord(angle + rotation, ratio);
|
2014-06-18 08:47:10 +02:00
|
|
|
min_w = min(min_w, tl.x);
|
|
|
|
max_w = max(max_w, tl.x);
|
|
|
|
min_h = min(min_h, tl.y);
|
|
|
|
max_h = max(max_h, tl.y);
|
2014-06-24 00:59:04 +02:00
|
|
|
// debug print
|
2014-06-24 19:14:59 +02:00
|
|
|
// if (rotation == 0.0)
|
|
|
|
// {
|
|
|
|
// cout << "Rotated " << p << " = " << tl << endl << endl;
|
|
|
|
// }
|
2014-06-18 08:47:10 +02:00
|
|
|
|
|
|
|
p = Point(src.width - 1, 0);
|
|
|
|
angle = convert_radian(src, p, ratio);
|
2014-06-22 15:50:42 +02:00
|
|
|
DPoint tr = convert_abs_coord(angle + rotation, ratio);
|
2014-06-18 08:47:10 +02:00
|
|
|
min_w = min(min_w, tr.x);
|
|
|
|
max_w = max(max_w, tr.x);
|
|
|
|
min_h = min(min_h, tr.y);
|
|
|
|
max_h = max(max_h, tr.y);
|
2014-06-24 19:14:59 +02:00
|
|
|
// if (rotation == 0.0)
|
|
|
|
// {
|
|
|
|
// cout << "Rotated " << p << " = " << tr << endl << endl;
|
|
|
|
// }
|
2014-06-18 08:47:10 +02:00
|
|
|
|
|
|
|
p = Point(0, src.height - 1);
|
|
|
|
angle = convert_radian(src, p, ratio);
|
2014-06-22 15:50:42 +02:00
|
|
|
DPoint bl = convert_abs_coord(angle + rotation, ratio);
|
2014-06-18 08:47:10 +02:00
|
|
|
min_w = min(min_w, bl.x);
|
|
|
|
max_w = max(max_w, bl.x);
|
|
|
|
min_h = min(min_h, bl.y);
|
|
|
|
max_h = max(max_h, bl.y);
|
2014-06-24 19:14:59 +02:00
|
|
|
// if (rotation == 0.0)
|
|
|
|
// {
|
|
|
|
// cout << "Rotated " << p << " = " << bl << endl << endl;
|
|
|
|
// }
|
2014-06-18 08:47:10 +02:00
|
|
|
|
|
|
|
p = Point(src.width - 1, src.height - 1);
|
|
|
|
angle = convert_radian(src, p, ratio);
|
2014-06-22 15:50:42 +02:00
|
|
|
DPoint br = convert_abs_coord(angle + rotation, ratio);
|
2014-06-18 08:47:10 +02:00
|
|
|
min_w = min(min_w, br.x);
|
|
|
|
max_w = max(max_w, br.x);
|
|
|
|
min_h = min(min_h, br.y);
|
|
|
|
max_h = max(max_h, br.y);
|
2014-06-24 19:14:59 +02:00
|
|
|
// if (rotation == 0.0)
|
|
|
|
// {
|
|
|
|
// cout << "Rotated " << p << " = " << br << endl << endl;
|
|
|
|
// }
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
width = (int) (max_w - min_w) + 1;
|
|
|
|
height = (int) (max_h - min_h) + 1;
|
2014-06-18 08:47:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
|
2014-06-19 00:05:49 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
//
|
2014-06-24 00:59:04 +02:00
|
|
|
// Point rotation
|
2014-06-19 00:05:49 +02:00
|
|
|
//
|
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
APoint rotate(Image const& src, Point const& p, double const ratio, double const rotation, Image const& rotated)
|
2014-06-19 00:05:49 +02:00
|
|
|
{
|
|
|
|
double angle = convert_radian(src, p, ratio);
|
2014-06-22 15:50:42 +02:00
|
|
|
DPoint a_point = convert_abs_coord(angle + rotation, ratio);
|
2014-06-19 00:05:49 +02:00
|
|
|
return convert_img_coord(rotated, a_point);
|
|
|
|
}
|
|
|
|
|
2014-06-24 19:14:59 +02:00
|
|
|
|
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Drawing
|
|
|
|
//
|
|
|
|
|
|
|
|
void draw_line(Image& img, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2)
|
|
|
|
{
|
2014-06-22 16:08:40 +02:00
|
|
|
int x_inc = x1 <= x2 ? 1 : -1;
|
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
unsigned int const y_min = min(y1, y2);
|
|
|
|
unsigned int const y_max = max(y1, y2);
|
2014-06-22 16:08:40 +02:00
|
|
|
double slope = (double) y2 - y1;
|
|
|
|
if (x1 != x2)
|
|
|
|
slope = ((double) y2 - y1) / abs((double) x2 - x1);
|
2014-06-22 15:50:42 +02:00
|
|
|
int y_inc = slope > 0 ? 1 : -1;
|
|
|
|
|
2014-06-22 16:08:40 +02:00
|
|
|
if (x1 == x2)
|
|
|
|
{
|
|
|
|
for (unsigned int runner = y1; runner != y2; runner+= y_inc)
|
|
|
|
img.set_pixel(x1, runner, 255, 0, 0); // set line to red
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y1 == y2)
|
|
|
|
{
|
|
|
|
for (unsigned int runner = x1; runner != x2; runner+= x_inc)
|
|
|
|
img.set_pixel(runner, y1, 255, 0, 0); // set line to red
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
unsigned int previous_y = y1;
|
2014-06-22 16:08:40 +02:00
|
|
|
for (unsigned int i = x1, steps = 0; i != x2; i += x_inc, ++steps)
|
2014-06-22 15:50:42 +02:00
|
|
|
{
|
2014-06-22 16:08:40 +02:00
|
|
|
unsigned int y = slope * steps + y1;
|
2014-06-22 15:50:42 +02:00
|
|
|
y = min(y, y_max);
|
|
|
|
y = max(y, y_min);
|
|
|
|
for (unsigned int runner = previous_y; runner != y; runner+= y_inc)
|
|
|
|
img.set_pixel(i, runner, 255, 0, 0); // set line to red
|
|
|
|
previous_y = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
void draw_line(Image& img, APoint const& p1, APoint const& p2)
|
2014-06-22 15:50:42 +02:00
|
|
|
{
|
|
|
|
draw_line(img, p1.x, p1.y, p2.x, p2.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw_outline(Image const& input, unsigned int degrees, string const& name)
|
|
|
|
{
|
|
|
|
double const rotation = (degrees / 180.0f) * M_PI;
|
|
|
|
unsigned int w = 0;
|
|
|
|
unsigned int h = 0;
|
|
|
|
compute_output_size(input, rotation, w, h);
|
|
|
|
cout << "rotation(" << degrees << ") -> " << w << " x " << h << endl;
|
|
|
|
Image rotated(w, h);
|
|
|
|
|
|
|
|
double const ratio = compute_ratio(input);
|
2014-06-24 00:59:04 +02:00
|
|
|
APoint tl = rotate(input, Point(0, 0), ratio, rotation, rotated);
|
|
|
|
APoint tr = rotate(input, Point(input.width - 1, 0), ratio, rotation, rotated);
|
|
|
|
APoint bl = rotate(input, Point(0, input.height - 1), ratio, rotation, rotated);
|
|
|
|
APoint br = rotate(input, Point(input.width - 1, input.height - 1), ratio, rotation, rotated);
|
2014-06-22 15:50:42 +02:00
|
|
|
cout << tl << " " << tr << " " << bl << " " << br << endl;
|
|
|
|
|
|
|
|
draw_line(rotated, tl, tr);
|
|
|
|
draw_line(rotated, tr, br);
|
|
|
|
draw_line(rotated, br, bl);
|
|
|
|
draw_line(rotated, bl, tl);
|
|
|
|
|
|
|
|
stringstream ss;
|
|
|
|
ss << "check_lines_" << name << "_" << degrees << ".ppm";
|
|
|
|
rotated.save(ss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Image rotation
|
|
|
|
//
|
|
|
|
|
2014-06-26 19:18:32 +02:00
|
|
|
DPoint get_mapped_point(Image const& src, APoint const& p, double const rotation)
|
|
|
|
{
|
|
|
|
DPoint const d = convert_grid_coord(src, p);
|
|
|
|
double p_angle = 0;
|
|
|
|
double dist = 0;
|
|
|
|
convert_abs_to_polar_coord(d, p_angle, dist);
|
|
|
|
return convert_polar_to_grid_coord(p_angle + rotation, dist);
|
|
|
|
}
|
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
Image rotate(Image const& src, double angle)
|
|
|
|
{
|
|
|
|
double const rotation = (angle / 180.0f) * M_PI;
|
|
|
|
unsigned int w = 0;
|
|
|
|
unsigned int h = 0;
|
|
|
|
compute_output_size(src, rotation, w, h);
|
|
|
|
Image rotated(w, h);
|
|
|
|
|
2014-06-26 19:18:32 +02:00
|
|
|
// corner points in rotated image
|
|
|
|
DPoint tl_grid = get_mapped_point(src, APoint(0, 0), rotation);
|
|
|
|
APoint tl = convert_img_coord(rotated, tl_grid);
|
|
|
|
DPoint tr_grid = get_mapped_point(src, APoint(src.width - 1, 0), rotation);
|
|
|
|
APoint tr = convert_img_coord(rotated, tr_grid);
|
|
|
|
DPoint bl_grid = get_mapped_point(src, APoint(0, src.height - 1), rotation);
|
|
|
|
APoint bl = convert_img_coord(rotated, bl_grid);
|
|
|
|
|
|
|
|
// corner points in source image
|
|
|
|
DPoint src_tl = get_mapped_point(rotated, tl, -rotation);
|
|
|
|
src_tl = convert_img_coord_precision(src, src_tl);
|
|
|
|
DPoint src_tr = get_mapped_point(rotated, tr, -rotation);
|
|
|
|
src_tr = convert_img_coord_precision(src, src_tr);
|
|
|
|
DPoint src_bl = get_mapped_point(rotated, bl, -rotation);
|
|
|
|
src_bl = convert_img_coord_precision(src, src_bl);
|
|
|
|
|
|
|
|
// steps for first column in source image
|
|
|
|
int origin_nb_steps = max(abs(bl.x - tl.x), abs(bl.y - tl.y));
|
|
|
|
double origin_y_inc = (src_bl.y - src_tl.y) / origin_nb_steps;
|
|
|
|
double origin_x_inc = (src_bl.x - src_tl.x) / origin_nb_steps;
|
|
|
|
|
|
|
|
// steps for line in source image
|
|
|
|
int line_nb_steps = max(abs(tr.x - tl.x), abs(tr.y - tl.y));
|
|
|
|
double line_y_inc = (src_tr.y - src_tl.y) / line_nb_steps;
|
|
|
|
double line_x_inc = (src_tr.x - src_tl.x) / line_nb_steps;
|
|
|
|
|
|
|
|
// steps for first column in rotated image
|
|
|
|
double rotated_y_inc = (bl.y - tl.y) / (float) origin_nb_steps;
|
|
|
|
double rotated_x_inc = (bl.x - tl.x) / (float) origin_nb_steps;
|
|
|
|
|
|
|
|
// steps for line in rotated image
|
|
|
|
DPoint bresenham((tr.x - tl.x) / (float) line_nb_steps, (tr.y - tl.y) / (float) line_nb_steps);
|
|
|
|
|
2014-06-26 21:28:34 +02:00
|
|
|
for (int y_i = 0; y_i <= (int) origin_nb_steps; ++y_i)
|
2014-06-24 00:59:04 +02:00
|
|
|
{
|
2014-06-26 19:18:32 +02:00
|
|
|
// first column origin
|
|
|
|
DPoint const src_origin(src_tl.x + y_i * origin_x_inc, src_tl.y + y_i * origin_y_inc);
|
|
|
|
APoint const rot_origin(tl.x + y_i * rotated_x_inc, tl.y + y_i * rotated_y_inc);
|
|
|
|
|
2014-06-26 23:45:02 +02:00
|
|
|
APoint previous = rot_origin;
|
|
|
|
|
2014-06-26 21:28:34 +02:00
|
|
|
for (int x_i = 0; x_i <= (int) line_nb_steps; ++x_i)
|
2014-06-24 00:59:04 +02:00
|
|
|
{
|
2014-06-26 19:18:32 +02:00
|
|
|
DPoint const src_rotated_point(src_origin.x + x_i * line_x_inc, src_origin.y + x_i * line_y_inc);
|
|
|
|
APoint const rot_point(rot_origin.x + x_i * bresenham.x, rot_origin.y + x_i * bresenham.y);
|
|
|
|
|
|
|
|
// if (y_i < 2 && (x_i == 0 || x_i == line_nb_steps - 1))
|
|
|
|
// {
|
|
|
|
// cout << " y_i[" << y_i << "] x_i[" << x_i << "]" << endl;
|
|
|
|
// cout << " src origin: " << src_origin << endl;
|
|
|
|
// cout << " src rotated point: " << src_rotated_point << endl;
|
|
|
|
// cout << " rotated origin: " << rot_origin << endl;
|
|
|
|
// cout << " rotated point: " << rot_point << endl;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// TODO: Interpolation would be done here
|
|
|
|
APoint src_p(src_rotated_point.x, src_rotated_point.y);
|
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
uint8_t r = 0;
|
|
|
|
uint8_t g = 0;
|
|
|
|
uint8_t b = 0;
|
2014-06-26 19:18:32 +02:00
|
|
|
// TODO: bypass variables, access src pixels
|
2014-06-24 00:59:04 +02:00
|
|
|
src.get_pixel(src_p, r, g, b);
|
2014-06-26 19:18:32 +02:00
|
|
|
rotated.set_pixel(rot_point, r, g, b);
|
2014-06-26 23:45:02 +02:00
|
|
|
|
|
|
|
// Fill missing points, created by interpolation
|
|
|
|
if (previous.x != rot_point.x && previous.y != rot_point.y)
|
|
|
|
{
|
|
|
|
rotated.set_pixel(APoint(rot_point.x, previous.y), r, g, b);
|
|
|
|
}
|
|
|
|
previous = rot_point;
|
2014-06-24 00:59:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rotated;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-19 00:05:49 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Check
|
|
|
|
//
|
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
bool check_points()
|
|
|
|
{
|
|
|
|
Image five(5, 5);
|
|
|
|
Point origin(0, 0);
|
|
|
|
DPoint d1 = convert_grid_coord(five, origin);
|
|
|
|
assert(d1.x == -2);
|
|
|
|
assert(d1.y == -2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:47:10 +02:00
|
|
|
bool check_trigo()
|
|
|
|
{
|
|
|
|
Image square(500, 500);
|
|
|
|
double const ratio = compute_ratio(square);
|
2014-06-24 19:14:59 +02:00
|
|
|
double const sigma = 1.0e-2;
|
|
|
|
if (!fequal(ratio, 1 / 707.106, sigma))
|
|
|
|
{
|
|
|
|
cerr << __LINE__ << " | Invalid ratio: " << ratio << " != " << 1 / 707.106 << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-18 08:47:10 +02:00
|
|
|
|
|
|
|
// Check that the origin of a square image is at sqrt(2) / 2
|
|
|
|
double const angle = convert_radian(square, Point(0, 0), ratio);
|
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
if (!fequal(angle, 3 * M_PI / 4, sigma))
|
2014-06-18 08:47:10 +02:00
|
|
|
{
|
2014-06-24 19:14:59 +02:00
|
|
|
cerr << __LINE__ << " | Invalid angle value: " << angle << " != " << 3 * M_PI / 4 << endl;
|
2014-06-18 08:47:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we can reverse the origin point.
|
2014-06-22 15:50:42 +02:00
|
|
|
DPoint const abs_reverse_point = convert_abs_coord(angle, ratio);
|
2014-06-24 00:59:04 +02:00
|
|
|
APoint const reverse_point = convert_img_coord(square, abs_reverse_point);
|
2014-06-24 19:14:59 +02:00
|
|
|
if (!fequal(0.0, reverse_point.x, sigma)
|
|
|
|
|| !fequal(0.0, reverse_point.y, sigma))
|
2014-06-18 08:47:10 +02:00
|
|
|
{
|
2014-06-24 19:14:59 +02:00
|
|
|
cerr << __LINE__ << "Reverse origin fail" << endl;
|
|
|
|
cerr << " " << reverse_point << " != (0, 0)" << endl;
|
|
|
|
cerr << " abs point " << abs_reverse_point << endl;
|
2014-06-18 08:47:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that when rotating the origin by 45 degrees
|
|
|
|
double const rotation = M_PI / 4; // 45 degrees
|
|
|
|
unsigned int w = 0;
|
|
|
|
unsigned int h = 0;
|
|
|
|
compute_output_size(square, rotation, w, h);
|
|
|
|
|
2014-06-24 19:14:59 +02:00
|
|
|
if (!fequal(w, square.width * sqrt(2), sigma * square.width)
|
|
|
|
|| !fequal(h, square.height * sqrt(2), sigma * square.height))
|
2014-06-19 00:05:49 +02:00
|
|
|
{
|
2014-06-24 19:14:59 +02:00
|
|
|
cerr << "Invalid rotated image dimensions " << w << " x " << h << endl;
|
|
|
|
cerr << " expected " << (int) ceil(square.width * sqrt(2)) << " x " << (int) ceil(square.height * sqrt(2)) << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-18 08:47:10 +02:00
|
|
|
|
|
|
|
|
2014-06-24 19:14:59 +02:00
|
|
|
Image rotated(w, h);
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-06-24 19:14:59 +02:00
|
|
|
DPoint const a_p45 = convert_abs_coord(angle + rotation, ratio);
|
|
|
|
APoint const p45 = convert_img_coord(rotated, a_p45);
|
|
|
|
if (!fequal(0, p45.x, sigma))
|
|
|
|
{
|
|
|
|
cerr << __LINE__ << " > Rotation origin by 45 degrees:" << endl;
|
|
|
|
cerr << " invalid x value: " << p45.x << " != " << 0 << endl;
|
|
|
|
cerr << " absolute point: " << a_p45 << endl;
|
|
|
|
cerr << " relative point: " << p45 << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!fequal(p45.y, (h - 1) / 2.0f, sigma))
|
|
|
|
{
|
|
|
|
cerr << __LINE__ << " > Rotation origin by 45 degrees:" << endl;
|
|
|
|
cerr << "Invalid y value: " << p45.y << " != " << (h - 1) / 2.0f << endl;
|
|
|
|
cerr << " absolute point: " << a_p45 << endl;
|
|
|
|
cerr << " relative point: " << p45 << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Polar coordinates
|
|
|
|
{
|
|
|
|
DPoint const d(-42.5, 37.5);
|
|
|
|
double angle = 0;
|
|
|
|
double dist = 0;
|
|
|
|
convert_abs_to_polar_coord(d, angle, dist);
|
|
|
|
DPoint const reversed = convert_polar_to_grid_coord(angle, dist);
|
|
|
|
if (!fequal(d.x, reversed.x, sigma)
|
|
|
|
|| !fequal(d.y, reversed.y, sigma))
|
2014-06-19 00:05:49 +02:00
|
|
|
{
|
2014-06-24 19:14:59 +02:00
|
|
|
cerr << __LINE__ << " > Reverse polar coordinates:" << endl;
|
|
|
|
cerr << reversed << " != " << d << endl;
|
|
|
|
cerr << "polar (" << angle << ", " << dist << ")" << endl;
|
2014-06-19 00:05:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-06-18 08:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-19 00:05:49 +02:00
|
|
|
void check_lines()
|
|
|
|
{
|
|
|
|
Image const square(500, 500);
|
2014-06-22 16:08:40 +02:00
|
|
|
draw_outline(square, 5, "square");
|
|
|
|
draw_outline(square, 12, "square");
|
2014-06-22 15:50:42 +02:00
|
|
|
draw_outline(square, 22, "square");
|
|
|
|
draw_outline(square, 33, "square");
|
|
|
|
draw_outline(square, 45, "square");
|
2014-06-22 16:08:40 +02:00
|
|
|
draw_outline(square, 60, "square");
|
|
|
|
draw_outline(square, 75, "square");
|
2014-06-22 15:50:42 +02:00
|
|
|
draw_outline(square, 90, "square");
|
|
|
|
|
|
|
|
Image const rect1(640, 480);
|
|
|
|
draw_outline(rect1, 22, "rect1");
|
|
|
|
draw_outline(rect1, 33, "rect1");
|
|
|
|
draw_outline(rect1, 45, "rect1");
|
|
|
|
draw_outline(rect1, 90, "rect1");
|
2014-06-18 08:47:10 +02:00
|
|
|
}
|
|
|
|
|
2014-06-26 23:45:02 +02:00
|
|
|
bool check_90(string const& path)
|
2014-06-26 21:28:34 +02:00
|
|
|
{
|
2014-06-26 23:45:02 +02:00
|
|
|
Image const src(path);
|
2014-06-26 21:28:34 +02:00
|
|
|
Image const rotated = rotate(src, 90);
|
|
|
|
|
|
|
|
for (unsigned int y = 0; y < rotated.height; ++y)
|
|
|
|
{
|
|
|
|
for (unsigned int x = 0; x < rotated.width; ++x)
|
|
|
|
{
|
|
|
|
unsigned rot_index = y * rotated.width + x;
|
|
|
|
unsigned src_index = x * src.width + (rotated.width - 1 - y);
|
|
|
|
|
|
|
|
if (rotated.r_chan[rot_index] != src.r_chan[src_index])
|
|
|
|
return false;
|
|
|
|
if (rotated.g_chan[rot_index] != src.g_chan[src_index])
|
|
|
|
return false;
|
|
|
|
if (rotated.b_chan[rot_index] != src.b_chan[src_index])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-06-22 15:50:42 +02:00
|
|
|
|
2014-06-18 08:47:10 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Main
|
|
|
|
//
|
|
|
|
|
2014-06-26 23:45:02 +02:00
|
|
|
int main(int argc, char* argv[])
|
2014-06-18 08:47:10 +02:00
|
|
|
{
|
2014-06-26 23:45:02 +02:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
cout << "Usage: " << argv[0] << " image.ppm" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-24 19:14:59 +02:00
|
|
|
bool perform_check = true;
|
2014-06-22 15:50:42 +02:00
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
if (perform_check)
|
|
|
|
{
|
|
|
|
if (!check_points())
|
|
|
|
return 1;
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
if (!check_trigo())
|
|
|
|
return 1;
|
2014-06-19 00:05:49 +02:00
|
|
|
|
2014-06-24 19:14:59 +02:00
|
|
|
//check_lines();
|
2014-06-26 21:28:34 +02:00
|
|
|
|
2014-06-26 23:45:02 +02:00
|
|
|
if (false && !check_90(argv[1]))
|
2014-06-26 21:28:34 +02:00
|
|
|
{
|
|
|
|
cerr << __LINE__ << " | 90 degrees check failed" << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-24 00:59:04 +02:00
|
|
|
}
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-06-26 23:45:02 +02:00
|
|
|
Image img(argv[1]);
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-06-26 23:45:02 +02:00
|
|
|
for (double rotation = 0; rotation <= 360; rotation += 5)
|
2014-06-24 00:59:04 +02:00
|
|
|
{
|
|
|
|
auto const before = chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
Image rotated = rotate(img, rotation);
|
|
|
|
|
|
|
|
auto const after = chrono::high_resolution_clock::now();
|
|
|
|
auto const duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(after - before);
|
2014-06-24 19:14:59 +02:00
|
|
|
cout << "rotate(" << rotation << "): " << duration_ms.count() << " ms" << endl;
|
2014-06-24 00:59:04 +02:00
|
|
|
|
|
|
|
stringstream filename;
|
2014-06-26 19:18:32 +02:00
|
|
|
filename << "rotated_";
|
|
|
|
if (rotation < 100)
|
|
|
|
filename << "0";
|
|
|
|
if (rotation < 10)
|
|
|
|
filename << "0";
|
|
|
|
filename << rotation << ".ppm";
|
2014-06-24 00:59:04 +02:00
|
|
|
rotated.save(filename.str());
|
|
|
|
}
|
2014-06-18 08:47:10 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|