2014-06-18 08:47:10 +02:00
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2014-07-07 23:31:59 +02:00
|
|
|
#include <iomanip>
|
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-07-03 23:53:06 +02:00
|
|
|
#include <cstdlib>
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-06-29 17:17:19 +02:00
|
|
|
#include <xmmintrin.h>
|
2014-07-03 23:53:06 +02:00
|
|
|
#include <emmintrin.h>
|
2014-07-27 22:40:03 +02:00
|
|
|
#include <tmmintrin.h>
|
2014-06-29 17:17:19 +02:00
|
|
|
|
2014-07-20 16:34:45 +02:00
|
|
|
#include "image.h"
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-07-20 16:34:45 +02:00
|
|
|
using namespace std;
|
2014-07-01 21:52:40 +02:00
|
|
|
|
2014-08-06 22:23:15 +02:00
|
|
|
#define LOG cout << __FUNCTION__ << ": " << __LINE__ << " | "
|
|
|
|
#define ERRLOG cerr << __FUNCTION__ << ": " << __LINE__ << " | "
|
2014-06-18 08:47:10 +02:00
|
|
|
|
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-28 13:52:24 +02:00
|
|
|
DPoint convert_grid_coord(Image const& img, Point 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-28 13:52:24 +02:00
|
|
|
double convert_radian(Image const& img, Point 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-28 13:52:24 +02:00
|
|
|
Point 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-28 13:52:24 +02:00
|
|
|
return Point(x, y);
|
2014-06-24 19:14:59 +02:00
|
|
|
}
|
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)
|
|
|
|
{
|
2014-07-07 23:19:57 +02:00
|
|
|
double x = p.x + (img.width / 2.0f) - 0.5;
|
|
|
|
double y = p.y + (img.height / 2.0f) - 0.5;
|
2014-06-26 19:18:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-28 13:52:24 +02:00
|
|
|
Point p(0, 0);
|
2014-06-18 08:47:10 +02:00
|
|
|
double angle = convert_radian(src, p, ratio);
|
2014-07-23 23:44:14 +02:00
|
|
|
DPoint const 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);
|
|
|
|
|
|
|
|
p = Point(src.width - 1, 0);
|
|
|
|
angle = convert_radian(src, p, ratio);
|
2014-07-23 23:44:14 +02:00
|
|
|
DPoint const 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);
|
|
|
|
|
|
|
|
p = Point(0, src.height - 1);
|
|
|
|
angle = convert_radian(src, p, ratio);
|
2014-07-23 23:44:14 +02:00
|
|
|
DPoint const 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);
|
|
|
|
|
|
|
|
p = Point(src.width - 1, src.height - 1);
|
|
|
|
angle = convert_radian(src, p, ratio);
|
2014-07-23 23:44:14 +02:00
|
|
|
DPoint const 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-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-08-05 22:13:20 +02:00
|
|
|
DPoint get_mapped_point(Image const& src, Point 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
|
|
|
|
2014-06-19 00:05:49 +02:00
|
|
|
|
2014-06-28 18:23:43 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Math approximation
|
|
|
|
//
|
|
|
|
|
|
|
|
void round_if_very_small(double& d)
|
|
|
|
{
|
|
|
|
if (abs(d) < 1.0e-10)
|
|
|
|
d = 0.0;
|
2014-07-02 21:54:52 +02:00
|
|
|
|
|
|
|
if (abs(d - 1) < 1.0e-10)
|
|
|
|
d = 1.0;
|
2014-06-28 18:23:43 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 23:48:11 +02:00
|
|
|
inline
|
|
|
|
bool fequal(float a, float b, float sigma)
|
|
|
|
{
|
|
|
|
return abs(a - b) < sigma;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-28 18:23:43 +02:00
|
|
|
|
2014-06-24 00:59:04 +02:00
|
|
|
//
|
|
|
|
//
|
2014-08-05 22:13:20 +02:00
|
|
|
// Padding
|
2014-06-24 00:59:04 +02:00
|
|
|
//
|
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
uint16_t* generate_padding_table(Image const& rotated,
|
|
|
|
Point src_rotated_origin,
|
|
|
|
Point const& qdx, Point const& qdy,
|
|
|
|
int src_qwidth, int src_qheight)
|
2014-06-26 19:18:32 +02:00
|
|
|
{
|
2014-08-05 22:13:20 +02:00
|
|
|
uint16_t* padding_table = new uint16_t[rotated.height];
|
|
|
|
// We suppose the image is square.
|
2014-08-06 21:49:40 +02:00
|
|
|
if (qdx.x == 0 || qdx.y == 0)
|
2014-08-05 22:13:20 +02:00
|
|
|
{
|
|
|
|
memset(padding_table, 0, sizeof (uint16_t) * rotated.height);
|
|
|
|
return padding_table;
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
for (unsigned int i = 0; i < rotated.height; ++i)
|
2014-08-05 22:13:20 +02:00
|
|
|
{
|
2014-08-06 21:49:40 +02:00
|
|
|
int y_range = 0;
|
|
|
|
if (src_rotated_origin.y < 0)
|
|
|
|
{
|
|
|
|
y_range = ceil((-src_rotated_origin.y) / (float) qdx.y);
|
|
|
|
}
|
|
|
|
else if (src_rotated_origin.y >= src_qheight)
|
|
|
|
{
|
|
|
|
y_range = ceil((float) (src_rotated_origin.y - src_qheight + 1) / (float) (-qdx.y));
|
|
|
|
}
|
|
|
|
if (y_range < 0)
|
|
|
|
{
|
|
|
|
cout << "Negative Y range" << endl;
|
|
|
|
abort();
|
|
|
|
}
|
2014-08-05 22:13:20 +02:00
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
int x_range = 0;
|
|
|
|
if (src_rotated_origin.x < 0)
|
|
|
|
{
|
|
|
|
x_range = ceil((-src_rotated_origin.x) / (float) qdx.x);
|
|
|
|
}
|
|
|
|
else if (src_rotated_origin.x >= src_qwidth)
|
|
|
|
{
|
|
|
|
x_range = ceil((float) (src_rotated_origin.x - src_qwidth + 1) / (float) (-qdx.x));
|
|
|
|
}
|
|
|
|
if (x_range < 0)
|
|
|
|
{
|
|
|
|
cout << "Negative X range" << endl;
|
|
|
|
abort();
|
|
|
|
}
|
2014-08-05 22:13:20 +02:00
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
padding_table[i] = max(x_range, y_range);
|
2014-08-05 22:13:20 +02:00
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
{
|
|
|
|
Point test(src_rotated_origin.x + padding_table[i] * qdx.x,
|
|
|
|
src_rotated_origin.y + padding_table[i] * qdx.y);
|
|
|
|
if (test.x < 0 || test.y < 0
|
|
|
|
|| test.x >= src_qwidth || test.y >= src_qheight)
|
|
|
|
{
|
|
|
|
cout << "Padding issue at line " << i << endl;
|
|
|
|
cout << " src: " << src_rotated_origin << endl;
|
|
|
|
cout << " test: " << test << endl;
|
|
|
|
cout << " q dim: " << src_qwidth << " x " << src_qheight << endl;
|
|
|
|
cout << " " << padding_table[i] << " x " << qdx << endl;
|
|
|
|
cout << " x diff = " << src_rotated_origin.x - src_qwidth + 1 << endl;
|
|
|
|
cout << " x coef = " << (float) (src_rotated_origin.x - src_qwidth + 1) / (float) (-qdx.x) << endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 22:13:20 +02:00
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
src_rotated_origin += qdy;
|
2014-08-05 22:13:20 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
return padding_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t* generate_padding_table_back_q(Image const& rotated,
|
|
|
|
Point src_rotated_origin,
|
|
|
|
Point const& qdx,
|
|
|
|
Point const& qdy,
|
|
|
|
int src_qwidth,
|
|
|
|
int src_qheight,
|
|
|
|
uint16_t const* front_padding)
|
|
|
|
{
|
|
|
|
uint16_t* padding_table = new uint16_t[rotated.height];
|
|
|
|
// We suppose the image is square.
|
|
|
|
if (qdx.x == 0 || qdx.y == 0)
|
2014-08-05 22:13:20 +02:00
|
|
|
{
|
2014-08-06 21:49:40 +02:00
|
|
|
memset(padding_table, 0, sizeof (uint16_t) * rotated.height);
|
|
|
|
return padding_table;
|
2014-08-05 22:13:20 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
src_rotated_origin.x = (rotated.width - 1) * qdx.x;
|
|
|
|
src_rotated_origin.y = (rotated.width - 1) * qdx.y;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < rotated.height; ++i)
|
|
|
|
{
|
|
|
|
int y_range = 0;
|
|
|
|
if (src_rotated_origin.y < 0)
|
|
|
|
{
|
|
|
|
y_range = ceil((-src_rotated_origin.y) / (float) qdx.y);
|
|
|
|
}
|
|
|
|
else if (src_rotated_origin.y >= src_qheight)
|
|
|
|
{
|
|
|
|
y_range = ceil((float) (src_rotated_origin.y - src_qheight + 1) / (float) (qdx.y));
|
|
|
|
}
|
|
|
|
if (y_range < 0)
|
|
|
|
{
|
|
|
|
cout << "Negative back Y range at line " << i << endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
int x_range = 0;
|
|
|
|
if (src_rotated_origin.x < 0)
|
|
|
|
{
|
|
|
|
x_range = ceil((-src_rotated_origin.x) / (float) qdx.x);
|
|
|
|
}
|
|
|
|
else if (src_rotated_origin.x >= src_qwidth)
|
|
|
|
{
|
|
|
|
x_range = ceil((float) (src_rotated_origin.x - src_qwidth + 1) / abs(qdx.x));
|
|
|
|
}
|
|
|
|
if (x_range < 0)
|
|
|
|
{
|
|
|
|
cout << "Negative back X range at line " << i << endl;
|
|
|
|
cout << " src origin: " << src_rotated_origin << endl;
|
|
|
|
cout << " q dim: " << src_qwidth << " x " << src_qheight << endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
padding_table[i] = max(x_range, y_range);
|
|
|
|
|
|
|
|
{
|
|
|
|
Point test(src_rotated_origin.x - padding_table[i] * qdx.x,
|
|
|
|
src_rotated_origin.y - padding_table[i] * qdx.y);
|
|
|
|
if (test.x < 0 || test.y < 0
|
|
|
|
|| test.x >= src_qwidth || test.y >= src_qheight)
|
|
|
|
{
|
|
|
|
padding_table[i] = rotated.width - front_padding[i];
|
|
|
|
// cout << "Back padding issue at line " << i << endl;
|
|
|
|
// cout << " src origin: " << src_rotated_origin << endl;
|
|
|
|
// cout << " test: " << test << endl;
|
|
|
|
// cout << " q dim: " << src_qwidth << " x " << src_qheight << endl;
|
|
|
|
// cout << " " << padding_table[i] << " x " << qdx << endl;
|
|
|
|
// cout << " X range " << x_range << endl;
|
|
|
|
// cout << " Y range " << y_range << endl;
|
|
|
|
// cout << " height diff = " << src_rotated_origin.y - src_qheight + 1 << endl;
|
|
|
|
// cout << " coef = " << (float) (src_rotated_origin.y - src_qheight + 1) / (float) qdx.y << endl;
|
|
|
|
// abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
src_rotated_origin += qdy;
|
|
|
|
}
|
2014-08-05 22:13:20 +02:00
|
|
|
|
|
|
|
return padding_table;
|
2014-06-26 19:18:32 +02:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:23:46 +02:00
|
|
|
void print_padding_table(uint16_t const* padding_table,
|
2014-08-05 22:42:31 +02:00
|
|
|
uint16_t const* border_table,
|
2014-08-06 21:49:40 +02:00
|
|
|
Image const& image)
|
2014-08-05 22:23:46 +02:00
|
|
|
{
|
|
|
|
cout << "Padding table:" << endl;
|
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
for (unsigned int i = 0; i < image.height; ++i)
|
2014-08-05 22:23:46 +02:00
|
|
|
{
|
|
|
|
int left_padding = padding_table[i];
|
2014-08-05 22:42:31 +02:00
|
|
|
int left_border = border_table[i];
|
2014-08-06 21:49:40 +02:00
|
|
|
int right_padding = padding_table[image.height - 1 - i];
|
|
|
|
int right_border = border_table[image.height - 1 - i];
|
|
|
|
int core_pixels = image.width - left_padding - right_padding - left_border - right_border;
|
2014-08-05 22:23:46 +02:00
|
|
|
|
|
|
|
if (core_pixels < 0)
|
|
|
|
{
|
2014-08-05 22:42:31 +02:00
|
|
|
cout << "Too much padding + border at line " << i << endl;
|
|
|
|
cout << " left padding = " << left_padding << endl;
|
|
|
|
cout << " left border = " << left_border << endl;
|
|
|
|
cout << " right border = " << right_border << endl;
|
2014-08-06 21:49:40 +02:00
|
|
|
cout << " right padding = " << right_padding << endl;
|
2014-08-05 22:42:31 +02:00
|
|
|
cout << " total = " << left_padding + left_border + right_border + right_padding << endl;
|
2014-08-06 21:49:40 +02:00
|
|
|
cout << " width = " << image.width << endl;
|
2014-08-05 22:23:46 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
cout << " " << i << " [";
|
2014-08-05 22:23:46 +02:00
|
|
|
for (int j = 0; j < left_padding; ++j)
|
|
|
|
cout << " ";
|
2014-08-05 22:42:31 +02:00
|
|
|
for (int j = 0; j < left_border; ++j)
|
|
|
|
cout << ".";
|
2014-08-05 22:23:46 +02:00
|
|
|
for (int j = 0; j < core_pixels; ++j)
|
|
|
|
cout << "x";
|
2014-08-05 22:42:31 +02:00
|
|
|
for (int j = 0; j < right_border; ++j)
|
|
|
|
cout << ".";
|
2014-08-05 22:23:46 +02:00
|
|
|
for (int j = 0; j < right_padding; ++j)
|
|
|
|
cout << " ";
|
|
|
|
cout << "]" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-05 22:36:48 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Border
|
|
|
|
//
|
2014-08-05 23:47:39 +02:00
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
uint16_t* generate_border_table(uint16_t const* padding_table, Image const& image)
|
2014-08-05 22:36:48 +02:00
|
|
|
{
|
2014-08-06 21:49:40 +02:00
|
|
|
uint16_t* border_table = new uint16_t[image.height];
|
|
|
|
if (image.width - padding_table[0] - padding_table[image.height - 1] == 0)
|
|
|
|
{
|
|
|
|
cout << "No room for top border" << endl;
|
|
|
|
border_table[0] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
border_table[0] = 1;
|
2014-08-05 22:36:48 +02:00
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
for (unsigned int i = 1; i < image.height - 1; ++i)
|
2014-08-05 22:36:48 +02:00
|
|
|
{
|
2014-08-06 21:49:40 +02:00
|
|
|
if (padding_table[i] == padding_table[i - 1])
|
2014-08-05 23:47:39 +02:00
|
|
|
{
|
2014-08-06 21:49:40 +02:00
|
|
|
border_table[i] = 1;
|
2014-08-05 23:47:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-06 21:49:40 +02:00
|
|
|
if (padding_table[i - 1] > padding_table[i])
|
|
|
|
{
|
|
|
|
border_table[i] = padding_table[i - 1] - padding_table[i] + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
border_table[i - 1] = padding_table[i] - padding_table[i - 1] + 1;
|
|
|
|
border_table[i] = 1;
|
|
|
|
}
|
2014-08-05 23:47:39 +02:00
|
|
|
}
|
2014-08-05 22:36:48 +02:00
|
|
|
}
|
|
|
|
|
2014-08-05 23:47:39 +02:00
|
|
|
// Check that we don't add too much border
|
2014-08-06 21:49:40 +02:00
|
|
|
for (unsigned int i = 1; i < image.height - 1; ++i)
|
2014-08-05 22:36:48 +02:00
|
|
|
{
|
|
|
|
if (padding_table[i] + border_table[i]
|
2014-08-06 21:49:40 +02:00
|
|
|
+ padding_table[image.height - 1 - i] + border_table[image.height - 1 - i]
|
|
|
|
> (int) image.width)
|
2014-08-05 22:36:48 +02:00
|
|
|
{
|
2014-08-06 21:49:40 +02:00
|
|
|
cout << "Too much border!" << endl;
|
|
|
|
cout << " border[" << i << "]: " << border_table[i] << endl;
|
|
|
|
cout << " border[" << image.height - 1 - i << "]: " << border_table[image.height - 1 - i] << endl;
|
|
|
|
if (border_table[i] > border_table[image.height - 1 - i])
|
|
|
|
border_table[i] -= 1;
|
|
|
|
else
|
|
|
|
border_table[image.height - 1 - i] -= 1;
|
2014-08-05 22:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
border_table[image.height - 1] = image.width - padding_table[0] - padding_table[image.height - 1] - border_table[0];
|
|
|
|
if (border_table[image.height - 1] > image.width)
|
|
|
|
{
|
|
|
|
cout << "That shit cray" << endl;
|
|
|
|
cout << " width = " << image.width << endl;
|
|
|
|
cout << " left padding = " << padding_table[0] << endl;
|
|
|
|
cout << " left border = " << border_table[0] << endl;
|
|
|
|
cout << " right border = " << border_table[image.height - 1] << endl;
|
|
|
|
cout << " right padding = " << padding_table[image.height - 1] << endl;
|
|
|
|
cout << " ~ " << image.width << endl;
|
|
|
|
cout << " ~ " << image.width - padding_table[0] << endl;
|
|
|
|
cout << " ~ " << image.width - padding_table[0] - padding_table[image.height - 1] << endl;
|
|
|
|
cout << " ~ " << image.width - padding_table[0] - padding_table[image.height - 1] - border_table[0] << endl;
|
|
|
|
}
|
2014-08-05 22:36:48 +02:00
|
|
|
|
|
|
|
return border_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-05 22:23:46 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Image rotation
|
|
|
|
//
|
|
|
|
|
2014-06-29 17:17:19 +02:00
|
|
|
inline
|
2014-07-07 23:19:57 +02:00
|
|
|
void rotate_pixel(Image const& src,
|
|
|
|
Point const& src_rotated_point,
|
2014-07-23 23:44:14 +02:00
|
|
|
pvalue_t* rotate_buffer, unsigned int rot_index,
|
2014-08-06 21:49:40 +02:00
|
|
|
int q_pow)
|
2014-06-28 13:52:24 +02:00
|
|
|
{
|
2014-07-24 22:13:47 +02:00
|
|
|
// Quantize on a 8x8 grid
|
|
|
|
int const q_inter_pow = 3;
|
|
|
|
int const q_inter = 1 << q_inter_pow;
|
|
|
|
int const mask = 0x07;
|
|
|
|
|
2014-07-23 23:44:14 +02:00
|
|
|
int const src_x = src_rotated_point.x >> q_pow;
|
|
|
|
int const src_y = src_rotated_point.y >> q_pow;
|
2014-07-07 23:19:57 +02:00
|
|
|
|
2014-06-28 18:23:43 +02:00
|
|
|
// Bilinear interpolation
|
2014-07-24 22:13:47 +02:00
|
|
|
unsigned int const src_index_1 = (src_y * src.width + src_x) * src.pixel_size;
|
2014-07-27 22:40:03 +02:00
|
|
|
unsigned int const src_index_2 = src_index_1 + src.pixel_size;
|
2014-07-24 22:13:47 +02:00
|
|
|
unsigned int const src_index_3 = src_index_1 + src.pixel_size * src.width;
|
|
|
|
unsigned int const src_index_4 = src_index_3 + src.pixel_size;
|
2014-06-28 18:35:48 +02:00
|
|
|
|
2014-07-27 22:40:03 +02:00
|
|
|
pvalue_t const src_tl = src.buffer[src_index_1];
|
|
|
|
pvalue_t const src_tr = src.buffer[src_index_2];
|
|
|
|
pvalue_t const src_bl = src.buffer[src_index_3];
|
|
|
|
pvalue_t const src_br = src.buffer[src_index_4];
|
|
|
|
|
2014-07-24 22:13:47 +02:00
|
|
|
unsigned int const x_delta = (src_rotated_point.x >> (q_pow - q_inter_pow)) & mask;
|
|
|
|
unsigned int const y_delta = (src_rotated_point.y >> (q_pow - q_inter_pow)) & mask;
|
|
|
|
unsigned int const inv_x = q_inter - x_delta;
|
|
|
|
unsigned int const inv_y = q_inter - y_delta;
|
2014-07-06 23:49:46 +02:00
|
|
|
|
2014-07-16 02:08:32 +02:00
|
|
|
#ifndef SIMD
|
|
|
|
|
2014-07-27 22:40:03 +02:00
|
|
|
pvalue_t interpolated = ((src_tl * inv_x + src_tr * x_delta) * inv_y
|
|
|
|
+ (src_bl * inv_x + src_br * x_delta) * y_delta) >> (q_inter_pow << 1);
|
|
|
|
rotate_buffer[rot_index] = interpolated;
|
2014-07-20 16:34:45 +02:00
|
|
|
// rotate_buffer[rot_index + 1] = ((src.buffer[src_index_1 + 1] * inv_x + src.buffer[src_index_2 + 1] * x_delta) * inv_y
|
|
|
|
// + (src.buffer[src_index_3 + 1] * inv_x + src.buffer[src_index_4 + 1] * x_delta) * y_delta) >> 6;
|
|
|
|
// rotate_buffer[rot_index + 2] = ((src.buffer[src_index_1 + 2] * inv_x + src.buffer[src_index_2 + 2] * x_delta) * inv_y
|
|
|
|
// + (src.buffer[src_index_3 + 2] * inv_x + src.buffer[src_index_4 + 2] * x_delta) * y_delta) >> 6;
|
2014-07-16 02:08:32 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// X-axis
|
|
|
|
__m128i top = _mm_loadu_si128((__m128i*) &src.buffer[src_index_1]);
|
|
|
|
__m128i bottom = _mm_loadu_si128((__m128i*) &src.buffer[src_index_3]);
|
|
|
|
__m128i coef = _mm_set_epi16(x_delta, x_delta, x_delta, x_delta, inv_x, inv_x, inv_x, inv_x);
|
|
|
|
top = _mm_mullo_epi16(top, coef);
|
|
|
|
bottom = _mm_mullo_epi16(bottom, coef);
|
|
|
|
|
|
|
|
// Y-axis
|
|
|
|
coef = _mm_set1_epi16(inv_y);
|
|
|
|
top = _mm_mullo_epi16(top, coef);
|
|
|
|
coef = _mm_set1_epi16(y_delta);
|
|
|
|
bottom = _mm_mullo_epi16(bottom, coef);
|
|
|
|
top = _mm_add_epi16(top, bottom);
|
|
|
|
|
2014-07-23 23:44:14 +02:00
|
|
|
top = _mm_srli_epi16(top, 2 * q_pow);
|
2014-07-16 02:08:32 +02:00
|
|
|
|
|
|
|
rotate_buffer[rot_index] = _mm_extract_epi16(top, 0) + _mm_extract_epi16(top, 4);
|
2014-07-20 16:34:45 +02:00
|
|
|
// rotate_buffer[rot_index + 1] = _mm_extract_epi16(top, 1) + _mm_extract_epi16(top, 5);
|
|
|
|
// rotate_buffer[rot_index + 2] = _mm_extract_epi16(top, 2) + _mm_extract_epi16(top, 6);
|
2014-07-16 02:08:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
#endif // ! SIMD
|
2014-06-28 13:52:24 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 23:51:41 +02:00
|
|
|
Image* rotate(Image const& src, double angle)
|
2014-06-24 00:59:04 +02:00
|
|
|
{
|
|
|
|
double const rotation = (angle / 180.0f) * M_PI;
|
|
|
|
unsigned int w = 0;
|
|
|
|
unsigned int h = 0;
|
|
|
|
compute_output_size(src, rotation, w, h);
|
2014-07-20 16:34:45 +02:00
|
|
|
Image* rotated = new Image(w, h, src.type);
|
2014-06-24 00:59:04 +02:00
|
|
|
|
2014-07-03 23:51:41 +02:00
|
|
|
DPoint const src_origin = get_mapped_point(*rotated, Point(0, 0), -rotation);
|
2014-08-06 22:24:20 +02:00
|
|
|
DPoint src_delta_x = get_mapped_point(*rotated, Point(src.width, 0), -rotation);
|
|
|
|
DPoint src_delta_y = get_mapped_point(*rotated, Point(0, src.height), -rotation);
|
2014-07-24 00:40:27 +02:00
|
|
|
src_delta_x -= src_origin;
|
|
|
|
src_delta_y -= src_origin;
|
2014-06-26 19:18:32 +02:00
|
|
|
|
2014-07-24 22:13:47 +02:00
|
|
|
// Quantized position on a grid
|
|
|
|
int const q_pos_pow = 10;
|
|
|
|
int const q_pos = 1 << q_pos_pow;
|
|
|
|
|
2014-07-23 23:44:14 +02:00
|
|
|
// TODO: we could have only one delta and deduce the other one
|
2014-08-06 22:24:20 +02:00
|
|
|
Point const qdx(ceil(src_delta_x.x * q_pos / src.width), ceil(src_delta_x.y * q_pos / src.width));
|
|
|
|
Point const qdy(ceil(src_delta_y.x * q_pos / src.height), ceil(src_delta_y.y * q_pos / src.height));
|
2014-06-28 13:52:24 +02:00
|
|
|
|
2014-07-04 08:23:06 +02:00
|
|
|
DPoint const rot_origin_in_src_grid = get_mapped_point(*rotated, Point(0, 0), -rotation);
|
|
|
|
DPoint const rot_origin_in_src = convert_img_coord_precision(src, rot_origin_in_src_grid);
|
2014-06-28 18:23:43 +02:00
|
|
|
|
2014-07-06 23:49:46 +02:00
|
|
|
unsigned int buffer_index = 0;
|
2014-07-16 02:08:32 +02:00
|
|
|
pvalue_t* buffer = rotated->buffer;
|
2014-06-28 18:23:43 +02:00
|
|
|
|
2014-07-27 22:40:03 +02:00
|
|
|
int const width = rotated->width;
|
|
|
|
int const height = rotated->height;
|
2014-08-06 21:49:40 +02:00
|
|
|
int const& src_qwidth = src.width * q_pos;
|
|
|
|
int const& src_qheight = src.height * q_pos;
|
2014-07-23 23:44:14 +02:00
|
|
|
|
2014-08-06 22:24:20 +02:00
|
|
|
Point src_rotated_origin(rot_origin_in_src.x * q_pos,
|
|
|
|
rot_origin_in_src.y * q_pos);
|
2014-08-05 22:23:46 +02:00
|
|
|
// Padding
|
2014-08-06 21:49:40 +02:00
|
|
|
uint16_t* padding_table = generate_padding_table(*rotated, src_rotated_origin,
|
|
|
|
qdx, qdy,
|
|
|
|
src_qwidth, src_qheight);
|
|
|
|
// uint16_t* back_padding_table = generate_padding_table_back_q(*rotated, src_rotated_origin,
|
|
|
|
// qdx, qdy,
|
|
|
|
// src_qwidth, src_qheight,
|
|
|
|
// padding_table);
|
|
|
|
//uint16_t* border_table = generate_border_table(padding_table, *rotated);
|
|
|
|
//print_padding_table(padding_table, border_table, *rotated);
|
2014-08-05 22:23:46 +02:00
|
|
|
|
2014-07-27 22:40:03 +02:00
|
|
|
for (int y = 0; y < height; ++y)
|
2014-07-04 08:23:06 +02:00
|
|
|
{
|
2014-06-28 18:23:43 +02:00
|
|
|
|
2014-08-05 23:47:39 +02:00
|
|
|
// TODO: compact these structure to increase locality
|
2014-08-05 22:27:18 +02:00
|
|
|
int const left_padding = padding_table[y];
|
2014-08-06 21:49:40 +02:00
|
|
|
int const left_border = 0;
|
|
|
|
int const right_padding = 0;
|
|
|
|
int const right_border = padding_table[height - 1 - y];
|
2014-08-05 23:47:39 +02:00
|
|
|
int const core_pixels = width - left_padding - left_border - right_border - right_padding;
|
2014-08-05 22:27:18 +02:00
|
|
|
|
|
|
|
// Left padding
|
|
|
|
for (int x = 0; x < left_padding; ++x, ++buffer_index)
|
|
|
|
{
|
|
|
|
// Set to black value
|
2014-08-06 21:49:40 +02:00
|
|
|
// TODO: memset
|
|
|
|
buffer[buffer_index] = 50;
|
2014-08-05 22:27:18 +02:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:36:48 +02:00
|
|
|
// Border
|
|
|
|
for (int x = 0; x < left_border; ++x, ++buffer_index)
|
|
|
|
{
|
|
|
|
buffer[buffer_index] = 0; // TODO: handle border
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
Point src_rotated_point(src_rotated_origin.x + (left_padding + left_border) * qdx.x,
|
|
|
|
src_rotated_origin.y + (left_padding + left_border) * qdx.y);
|
|
|
|
|
2014-08-05 22:27:18 +02:00
|
|
|
// Body
|
|
|
|
for (int x = 0; x < core_pixels; ++x, ++buffer_index)
|
2014-07-04 08:23:06 +02:00
|
|
|
{
|
2014-08-06 21:49:40 +02:00
|
|
|
if (src_rotated_point.x < 0 || src_rotated_point.y < 0
|
|
|
|
|| src_rotated_point.x >= src_qwidth || src_rotated_point.y >= src_qheight)
|
|
|
|
{
|
2014-08-06 22:33:41 +02:00
|
|
|
LOG << "Out-of-bounds point!" << endl;
|
2014-08-06 21:49:40 +02:00
|
|
|
// cout << " x: " << x << endl;
|
|
|
|
// cout << " src_rotated_point: " << src_rotated_point << endl;
|
|
|
|
// cout << " qdx: " << qdx << endl;
|
|
|
|
// cout << " q_pos: " << q_pos << endl;
|
|
|
|
// cout << " rotated point: (" << x + left_padding + left_border << ", " << y << ")" << endl;
|
|
|
|
// cout << " src point: (" << (src_rotated_point.x >> q_pos_pow) << ", " << (src_rotated_point.y >> q_pos_pow) << ")" << endl;
|
|
|
|
// cout << " src point: (" << (src_rotated_point.x / q_pos) << ", " << (src_rotated_point.y / q_pos) << ")" << endl;
|
|
|
|
// cout << " width: " << width << endl;
|
|
|
|
// cout << " left padding: " << left_padding << endl;
|
|
|
|
// cout << " left border: " << left_border << endl;
|
|
|
|
// cout << " core pixels: " << core_pixels << endl;
|
|
|
|
// cout << " right border: " << right_border << endl;
|
|
|
|
// cout << " right padding: " << right_padding << endl;
|
|
|
|
buffer[buffer_index] = 255;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-06 22:33:41 +02:00
|
|
|
rotate_pixel(src, src_rotated_point, buffer, buffer_index, q_pos_pow);
|
2014-08-06 21:49:40 +02:00
|
|
|
}
|
|
|
|
|
2014-07-24 00:40:27 +02:00
|
|
|
src_rotated_point += qdx;
|
2014-08-05 22:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Border
|
|
|
|
for (int x = 0; x < right_border; ++x, ++buffer_index)
|
|
|
|
{
|
|
|
|
buffer[buffer_index] = 0; // TODO: handle border
|
|
|
|
src_rotated_point += qdx;
|
2014-06-24 00:59:04 +02:00
|
|
|
}
|
2014-08-05 22:27:18 +02:00
|
|
|
|
|
|
|
// Right padding
|
|
|
|
for (int x = 0; x < right_padding; ++x, ++buffer_index)
|
|
|
|
{
|
|
|
|
// Set to black value
|
2014-08-06 21:49:40 +02:00
|
|
|
// TODO: memset
|
|
|
|
buffer[buffer_index] = 100;
|
2014-08-05 22:27:18 +02:00
|
|
|
}
|
|
|
|
|
2014-08-05 22:19:07 +02:00
|
|
|
src_rotated_origin += qdy;
|
2014-06-24 00:59:04 +02:00
|
|
|
}
|
2014-08-06 21:49:40 +02:00
|
|
|
// cout << endl;
|
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()
|
|
|
|
{
|
2014-07-20 16:34:45 +02:00
|
|
|
Image five(5, 5, pnm::Format::PGM);
|
2014-06-22 15:50:42 +02:00
|
|
|
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()
|
|
|
|
{
|
2014-07-20 16:34:45 +02:00
|
|
|
Image square(500, 500, pnm::Format::PGM);
|
2014-06-18 08:47:10 +02:00
|
|
|
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-28 13:52:24 +02:00
|
|
|
Point 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-07-20 16:34:45 +02:00
|
|
|
Image rotated(w, h, pnm::Format::PGM);
|
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);
|
2014-06-28 13:52:24 +02:00
|
|
|
Point const p45 = convert_img_coord(rotated, a_p45);
|
2014-06-24 19:14:59 +02:00
|
|
|
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-08-06 22:23:40 +02:00
|
|
|
bool check_00(string const& path)
|
|
|
|
{
|
|
|
|
Image const src(path);
|
|
|
|
Image const* rotated = rotate(src, 0);
|
|
|
|
|
|
|
|
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) * rotated->pixel_size;
|
|
|
|
unsigned src_index = (y * src.width + x) * src.pixel_size;
|
|
|
|
if (memcmp(&rotated->buffer[rot_index], &src.buffer[src_index], src.pixel_size * sizeof (pvalue_t)) != 0)
|
|
|
|
{
|
|
|
|
Point r(x, y);
|
|
|
|
Point s(x, y);
|
|
|
|
LOG << "R" << r << " != S" << s << endl;
|
|
|
|
LOG << "R: " << rot_index << " != S: " << src_index << endl;
|
|
|
|
LOG << rotated->buffer[rot_index] << " != " << src.buffer[src_index] << endl;
|
|
|
|
LOG << "R dim: " << rotated->width << " x " << rotated->height << endl;
|
|
|
|
LOG << "S dim: " << src.width << " x " << src.height << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete rotated;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-07-03 23:51:41 +02:00
|
|
|
Image const* rotated = rotate(src, 90);
|
2014-06-26 21:28:34 +02:00
|
|
|
|
2014-07-03 23:51:41 +02:00
|
|
|
for (unsigned int y = 0; y < rotated->height; ++y)
|
2014-06-26 21:28:34 +02:00
|
|
|
{
|
2014-07-03 23:51:41 +02:00
|
|
|
for (unsigned int x = 0; x < rotated->width; ++x)
|
2014-06-26 21:28:34 +02:00
|
|
|
{
|
2014-07-20 16:34:45 +02:00
|
|
|
unsigned rot_index = (y * rotated->width + x) * rotated->pixel_size;
|
|
|
|
unsigned src_index = (x * src.width + (src.width - 1 - y)) * src.pixel_size;
|
|
|
|
if (memcmp(&rotated->buffer[rot_index], &src.buffer[src_index], src.pixel_size * sizeof (pvalue_t)) != 0)
|
2014-06-28 13:52:24 +02:00
|
|
|
{
|
|
|
|
Point r(x, y);
|
|
|
|
Point s((src.width - 1 - y), x);
|
|
|
|
cerr << __LINE__ << " | R: " << r << " != S:" << s << endl;
|
2014-07-03 23:51:41 +02:00
|
|
|
cerr << "R dim: " << rotated->width << " x " << rotated->height << endl;
|
2014-06-28 13:52:24 +02:00
|
|
|
cerr << "S dim: " << src.width << " x " << src.height << endl;
|
2014-06-26 21:28:34 +02:00
|
|
|
return false;
|
2014-06-28 13:52:24 +02:00
|
|
|
}
|
2014-06-26 21:28:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 23:51:41 +02:00
|
|
|
delete rotated;
|
|
|
|
|
2014-06-26 21:28:34 +02:00
|
|
|
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-07-03 23:49:10 +02:00
|
|
|
string get_save_path(string const& base, unsigned int i)
|
|
|
|
{
|
|
|
|
stringstream filename;
|
|
|
|
//filename << "/tmp/";
|
|
|
|
filename << base << "_";
|
|
|
|
|
|
|
|
if (i < 100)
|
|
|
|
filename << "0";
|
|
|
|
if (i < 10)
|
|
|
|
filename << "0";
|
2014-07-20 16:34:45 +02:00
|
|
|
filename << i << ".pnm";
|
2014-07-03 23:49:10 +02:00
|
|
|
|
|
|
|
return filename.str();
|
|
|
|
}
|
2014-06-18 08:47:10 +02:00
|
|
|
|
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-08-06 22:23:40 +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-08-06 22:23:40 +02:00
|
|
|
if (!check_00(argv[1]))
|
|
|
|
{
|
|
|
|
ERRLOG << "0 degree check failed" << endl << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-28 13:52:24 +02:00
|
|
|
if (!check_90(argv[1]))
|
2014-06-26 21:28:34 +02:00
|
|
|
{
|
2014-08-06 22:23:15 +02:00
|
|
|
ERRLOG << "90 degrees check failed" << endl << endl;
|
|
|
|
return 1;
|
2014-06-26 21:28:34 +02:00
|
|
|
}
|
2014-06-24 00:59:04 +02:00
|
|
|
}
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-08-06 21:49:40 +02:00
|
|
|
double const step = 5;
|
|
|
|
bool save_output_img = true;
|
|
|
|
bool print_each_run = true;
|
2014-06-18 08:47:10 +02:00
|
|
|
|
2014-07-07 23:31:59 +02:00
|
|
|
// No tile
|
2014-07-08 08:04:43 +02:00
|
|
|
Image img(argv[1]);
|
2014-07-07 23:31:59 +02:00
|
|
|
float average = 0.0;
|
|
|
|
int i = 0;
|
2014-07-10 22:01:30 +02:00
|
|
|
for (double rotation = 0; rotation < 360; rotation += step)
|
2014-06-24 00:59:04 +02:00
|
|
|
{
|
|
|
|
auto const before = chrono::high_resolution_clock::now();
|
2014-07-03 23:51:41 +02:00
|
|
|
Image* const rotated = rotate(img, rotation);
|
2014-06-24 00:59:04 +02:00
|
|
|
auto const after = chrono::high_resolution_clock::now();
|
|
|
|
auto const duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(after - before);
|
2014-07-07 23:31:59 +02:00
|
|
|
average += duration_ms.count();
|
2014-07-02 21:54:52 +02:00
|
|
|
|
2014-07-16 20:44:32 +02:00
|
|
|
if (print_each_run)
|
|
|
|
cout << "rotate(" << rotation << "): " << duration_ms.count() << " ms" << endl;
|
|
|
|
|
|
|
|
if (save_output_img)
|
|
|
|
rotated->save(get_save_path("rotated", rotation));
|
2014-07-02 21:54:52 +02:00
|
|
|
|
2014-07-07 23:31:59 +02:00
|
|
|
delete rotated;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
cout << "---------" << endl;
|
|
|
|
cout << " average: " << average / i << "ms" << endl << endl;
|
|
|
|
|
2014-06-18 08:47:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|