Use a unique buffer for pixels (scanline).

- Add rotate_pixel().
- Remove APoint, use Point instead.
master
Fabien Freling 2014-06-28 13:52:24 +02:00
parent 6a94c79ac2
commit 258ddce814
2 changed files with 104 additions and 126 deletions

View File

@ -6,3 +6,12 @@
[ ] Fix out-of-bounds pixel set
[ ] Optimization for square images?
[ ] Fixed point computation?
# Cache
[X] Rotate per channel -> no gain
[ ] Load pixels in uint64-t
[ ] Cut image in tiles
# Quality
[ ] Interpolate using SIMD, SSE

View File

@ -21,7 +21,6 @@ struct TPoint {
{}
};
typedef TPoint<int> Point;
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)
@ -33,28 +32,20 @@ std::basic_ostream<Elem, Traits>& operator << (std::basic_ostream<Elem, Traits>&
struct Image {
unsigned int width;
unsigned int height;
uint8_t* r_chan;
uint8_t* g_chan;
uint8_t* b_chan;
uint8_t* buffer;
Image()
: width(0)
, height(0)
, r_chan(NULL)
, g_chan(NULL)
, b_chan(NULL)
, buffer(NULL)
{}
Image(unsigned int w, unsigned int h)
{
this->width = w;
this->height = h;
r_chan = new uint8_t[width * height];
memset(r_chan, 255, width * height * sizeof (uint8_t));
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));
buffer = new uint8_t[width * height * 3];
memset(buffer, 0, width * height * 3 * sizeof (uint8_t));
}
Image(string const& path)
@ -75,12 +66,8 @@ struct Image {
if (!this->read_body(is))
{
delete r_chan;
r_chan = nullptr;
delete g_chan;
r_chan = nullptr;
delete b_chan;
r_chan = nullptr;
delete buffer;
buffer = nullptr;
cerr << "Invalid header." << endl;
abort();
@ -104,15 +91,15 @@ struct Image {
{
if (x >= width || y >= height)
{
cerr << __LINE__ << " | Point (" << x << ", " << y << ") out of bounds" << endl;
cerr << " Image dimensions: " << width << " x " << height << endl;
// cerr << __LINE__ << " | Point (" << x << ", " << y << ") out of bounds" << endl;
// cerr << " Image dimensions: " << width << " x " << height << endl;
// assert(false);
return;
}
int const index = y * width + x;
r_chan[index] = r;
g_chan[index] = g;
b_chan[index] = b;
int index = (y * width + x) * 3;
buffer[index++] = r;
buffer[index++] = g;
buffer[index++] = b;
}
void set_pixel(Point const& p, uint8_t r, uint8_t g, uint8_t b)
@ -120,22 +107,6 @@ struct Image {
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];
}
private:
bool read_header(std::ifstream& istr)
@ -205,19 +176,14 @@ struct Image {
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];
unsigned int const nb_pixels = width * height;
buffer = new uint8_t[nb_pixels * 3];
for (unsigned int row = 0; row < height; ++row)
uint8_t* buf_index = buffer;
for (unsigned int i = 0; i < nb_pixels * 3; ++i)
{
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();
}
*buf_index = istr.get();
++buf_index;
}
return true;
@ -225,16 +191,14 @@ struct Image {
bool write_body(std::ofstream& ostr)
{
for (unsigned int row = 0; row < height; ++row)
unsigned int const nb_pixels = width * height;
uint8_t* buf_index = buffer;
for (unsigned int i = 0; i < nb_pixels * 3; ++i)
{
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];
}
ostr << (char) *buf_index;
++buf_index;
}
return true;
}
};
@ -246,12 +210,12 @@ struct Image {
// Trigonometry
//
DPoint convert_grid_coord(Image const& img, APoint const& p)
DPoint convert_grid_coord(Image const& img, Point const& p)
{
return DPoint(p.x - img.width / 2.0f + 0.5, p.y - img.height / 2.0f + 0.5);
}
double convert_radian(Image const& img, APoint const& p, double const ratio)
double convert_radian(Image const& img, Point const& p, double const ratio)
{
DPoint centered = convert_grid_coord(img, p);
double const cos_value = centered.x * ratio;
@ -270,11 +234,11 @@ DPoint convert_abs_coord(double const angle, double const ratio)
return DPoint(cos(angle) / ratio, - sin(angle) / ratio);
}
APoint convert_img_coord(Image const& img, DPoint const& p)
Point convert_img_coord(Image const& img, DPoint const& p)
{
int x = round(p.x + (img.width / 2.0f) - 0.5);
int y = round(p.y + (img.height / 2.0f) - 0.5);
return APoint(x, y);
return Point(x, y);
}
DPoint convert_img_coord_precision(Image const& img, DPoint const& p)
@ -316,7 +280,7 @@ void compute_output_size(Image const& src, double const rotation, unsigned int&
double max_h = 0;
//cout << "Image dimensions: " << src.width << " x " << src.height << endl;
APoint p(0, 0);
Point p(0, 0);
double angle = convert_radian(src, p, ratio);
DPoint tl = convert_abs_coord(angle + rotation, ratio);
min_w = min(min_w, tl.x);
@ -376,7 +340,7 @@ void compute_output_size(Image const& src, double const rotation, unsigned int&
// Point rotation
//
APoint rotate(Image const& src, Point const& p, double const ratio, double const rotation, Image const& rotated)
Point rotate(Image const& src, Point const& p, double const ratio, double const rotation, Image const& rotated)
{
double angle = convert_radian(src, p, ratio);
DPoint a_point = convert_abs_coord(angle + rotation, ratio);
@ -427,7 +391,7 @@ void draw_line(Image& img, unsigned int x1, unsigned int y1, unsigned int x2, un
}
}
void draw_line(Image& img, APoint const& p1, APoint const& p2)
void draw_line(Image& img, Point const& p1, Point const& p2)
{
draw_line(img, p1.x, p1.y, p2.x, p2.y);
}
@ -442,10 +406,10 @@ void draw_outline(Image const& input, unsigned int degrees, string const& name)
Image rotated(w, h);
double const ratio = compute_ratio(input);
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);
Point tl = rotate(input, Point(0, 0), ratio, rotation, rotated);
Point tr = rotate(input, Point(input.width - 1, 0), ratio, rotation, rotated);
Point bl = rotate(input, Point(0, input.height - 1), ratio, rotation, rotated);
Point br = rotate(input, Point(input.width - 1, input.height - 1), ratio, rotation, rotated);
cout << tl << " " << tr << " " << bl << " " << br << endl;
draw_line(rotated, tl, tr);
@ -465,7 +429,7 @@ void draw_outline(Image const& input, unsigned int degrees, string const& name)
// Image rotation
//
DPoint get_mapped_point(Image const& src, APoint const& p, double const rotation)
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;
@ -474,6 +438,30 @@ DPoint get_mapped_point(Image const& src, APoint const& p, double const rotation
return convert_polar_to_grid_coord(p_angle + rotation, dist);
}
void rotate_pixel(Image const& src, Image& rotated,
DPoint const& src_rotated_point, Point const& rot_point,
unsigned int const src_limit, unsigned int const rot_limit,
Point const& previous)
{
// TODO: Interpolation would be done here
// For now we take the nearest point
unsigned int src_index = ((int) src_rotated_point.y * src.width + (int) src_rotated_point.x) * 3;
unsigned int rot_index = (rot_point.y * rotated.width + rot_point.x) * 3;
if (src_index >= src_limit
|| rot_index >= rot_limit)
return;
memcpy(&rotated.buffer[rot_index], &src.buffer[src_index], 3 * sizeof (uint8_t));
// Fill missing points, created by interpolation
if (previous.x != rot_point.x && previous.y != rot_point.y)
{
src_index -= 3;
unsigned int previous_index = (previous.y * rotated.width + rot_point.x) * 3;
memcpy(&rotated.buffer[previous_index], &src.buffer[src_index], 3 * sizeof (uint8_t));
}
}
Image rotate(Image const& src, double angle)
{
double const rotation = (angle / 180.0f) * M_PI;
@ -483,12 +471,12 @@ Image rotate(Image const& src, double angle)
Image rotated(w, h);
// 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);
DPoint tl_grid = get_mapped_point(src, Point(0, 0), rotation);
Point tl = convert_img_coord(rotated, tl_grid);
DPoint tr_grid = get_mapped_point(src, Point(src.width - 1, 0), rotation);
Point tr = convert_img_coord(rotated, tr_grid);
DPoint bl_grid = get_mapped_point(src, Point(0, src.height - 1), rotation);
Point bl = convert_img_coord(rotated, bl_grid);
// corner points in source image
DPoint src_tl = get_mapped_point(rotated, tl, -rotation);
@ -500,58 +488,37 @@ Image rotate(Image const& src, double angle)
// 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;
DPoint origin_step((src_bl.x - src_tl.x) / origin_nb_steps, (src_bl.y - src_tl.y) / 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;
DPoint line_step((src_tr.x - src_tl.x) / line_nb_steps, (src_tr.y - src_tl.y) / 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;
DPoint rotated_step((bl.x - tl.x) / (float) origin_nb_steps, (bl.y - tl.y) / (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);
unsigned int const src_limit = src.width * src.height * 3;
unsigned int const rot_limit = rotated.width * rotated.height * 3;
for (int y_i = 0; y_i <= (int) origin_nb_steps; ++y_i)
{
// 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);
DPoint const src_origin(src_tl.x + y_i * origin_step.x, src_tl.y + y_i * origin_step.y);
Point const rot_origin(tl.x + y_i * rotated_step.x, tl.y + y_i * rotated_step.y);
APoint previous = rot_origin;
Point previous = rot_origin;
for (int x_i = 0; x_i <= (int) line_nb_steps; ++x_i)
{
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);
DPoint const src_rotated_point(src_origin.x + x_i * line_step.x, src_origin.y + x_i * line_step.y);
Point const rot_point(rot_origin.x + x_i * bresenham.x, rot_origin.y + x_i * bresenham.y);
// cout << "src rot point: " << src_rotated_point << endl;
// cout << "rot point: " << rot_point << endl;
// 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);
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
// TODO: bypass variables, access src pixels
src.get_pixel(src_p, r, g, b);
rotated.set_pixel(rot_point, r, g, b);
// 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);
}
rotate_pixel(src, rotated, src_rotated_point, rot_point, src_limit, rot_limit, previous);
previous = rot_point;
}
}
@ -599,7 +566,7 @@ bool check_trigo()
// Check that we can reverse the origin point.
DPoint const abs_reverse_point = convert_abs_coord(angle, ratio);
APoint const reverse_point = convert_img_coord(square, abs_reverse_point);
Point const reverse_point = convert_img_coord(square, abs_reverse_point);
if (!fequal(0.0, reverse_point.x, sigma)
|| !fequal(0.0, reverse_point.y, sigma))
{
@ -627,7 +594,7 @@ bool check_trigo()
Image rotated(w, h);
DPoint const a_p45 = convert_abs_coord(angle + rotation, ratio);
APoint const p45 = convert_img_coord(rotated, a_p45);
Point const p45 = convert_img_coord(rotated, a_p45);
if (!fequal(0, p45.x, sigma))
{
cerr << __LINE__ << " > Rotation origin by 45 degrees:" << endl;
@ -693,15 +660,17 @@ bool check_90(string const& path)
{
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])
unsigned rot_index = (y * rotated.width + x) * 3;
unsigned src_index = (x * src.width + (src.width - 1 - y)) * 3;
if (memcmp(&rotated.buffer[rot_index], &src.buffer[src_index], 3 * sizeof (uint8_t)) != 0)
{
Point r(x, y);
Point s((src.width - 1 - y), x);
cerr << __LINE__ << " | R: " << r << " != S:" << s << endl;
cerr << "R dim: " << rotated.width << " x " << rotated.height << endl;
cerr << "S dim: " << src.width << " x " << src.height << endl;
return false;
}
}
}
@ -735,7 +704,7 @@ int main(int argc, char* argv[])
//check_lines();
if (false && !check_90(argv[1]))
if (!check_90(argv[1]))
{
cerr << __LINE__ << " | 90 degrees check failed" << endl;
return 1;
@ -744,7 +713,7 @@ int main(int argc, char* argv[])
Image img(argv[1]);
for (double rotation = 0; rotation <= 360; rotation += 5)
for (double rotation = 2; rotation < 360; rotation += 400)
{
auto const before = chrono::high_resolution_clock::now();
@ -755,7 +724,7 @@ int main(int argc, char* argv[])
cout << "rotate(" << rotation << "): " << duration_ms.count() << " ms" << endl;
stringstream filename;
filename << "rotated_";
filename << "/tmp/rotated_";
if (rotation < 100)
filename << "0";
if (rotation < 10)