Add borders.

This commit is contained in:
Fabien Freling 2014-08-07 01:24:29 +02:00
parent bd84e85c1e
commit 4f46d32b9e

View file

@ -345,32 +345,29 @@ void print_padding_table(uint16_t const* padding_table,
// Border
//
uint16_t* generate_border_table(uint16_t const* padding_table, Image const& image)
uint16_t* generate_border_table(uint16_t const* front_padding,
uint16_t const* back_padding,
Image const& image)
{
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;
border_table[0] = image.width - front_padding[0] - back_padding[0];
for (unsigned int i = 1; i < image.height - 1; ++i)
{
if (padding_table[i] == padding_table[i - 1])
if (front_padding[i] == front_padding[i - 1])
{
border_table[i] = 1;
}
else
{
if (padding_table[i - 1] > padding_table[i])
if (front_padding[i - 1] > front_padding[i])
{
border_table[i] = padding_table[i - 1] - padding_table[i] + 1;
border_table[i] = front_padding[i - 1] - front_padding[i] + 1;
}
else
{
border_table[i - 1] = padding_table[i] - padding_table[i - 1] + 1;
border_table[i - 1] = front_padding[i] - front_padding[i - 1] + 1;
border_table[i] = 1;
}
}
@ -379,39 +376,62 @@ uint16_t* generate_border_table(uint16_t const* padding_table, Image const& imag
// Check that we don't add too much border
for (unsigned int i = 1; i < image.height - 1; ++i)
{
if (padding_table[i] + border_table[i]
+ padding_table[image.height - 1 - i] + border_table[image.height - 1 - i]
> (int) image.width)
while (front_padding[i] + border_table[i] + back_padding[i] > (int) image.width)
{
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;
border_table[i] -= 1;
}
}
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)
{
LOG << "That shit cray" << endl;
LOG << " width = " << image.width << endl;
LOG << " left padding = " << padding_table[0] << endl;
LOG << " left border = " << border_table[0] << endl;
LOG << " right border = " << border_table[image.height - 1] << endl;
LOG << " right padding = " << padding_table[image.height - 1] << endl;
LOG << " ~ width ~ " << image.width << endl;
LOG << " ~ width - left padding ~ " << image.width - padding_table[0] << endl;
LOG << " ~ width - padding ~ " << image.width - padding_table[0] - padding_table[image.height - 1] << endl;
LOG << " ~ width - padding - left border ~ " << image.width - padding_table[0] - padding_table[image.height - 1] - border_table[0] << endl;
}
border_table[image.height - 1] = image.width - front_padding[image.height - 1] - back_padding[image.height - 1];
return border_table;
}
uint16_t* generate_border_table_back(uint16_t const* front_padding,
uint16_t const* front_border,
uint16_t const* back_padding,
Image const& image)
{
uint16_t* back_border = new uint16_t[image.height];
back_border[0] = 0;
for (unsigned int i = 1; i < image.height - 1; ++i)
{
if (back_padding[i] == back_padding[i - 1])
{
back_border[i] = 1;
}
else
{
if (back_padding[i - 1] > back_padding[i])
{
back_border[i] = back_padding[i - 1] - back_padding[i] + 1;
}
else
{
back_border[i - 1] = back_padding[i] - back_padding[i - 1] + 1;
back_border[i] = 1;
}
}
}
back_border[0] = 0;
// Check that we don't add too much border
for (unsigned int i = 1; i < image.height - 1; ++i)
{
while (front_padding[i] + front_border[i] + back_border[i] + back_padding[i] > (int) image.width)
{
back_border[i] -= 1;
}
}
back_border[image.height - 1] = 0;
return back_border;
}
//
@ -541,19 +561,31 @@ Image* rotate(Image const& src, double angle)
qdx, qdy,
src_qwidth, src_qheight,
padding_table);
//uint16_t* border_table = generate_border_table(padding_table, *rotated);
uint16_t* front_border = generate_border_table(padding_table, back_padding_table, *rotated);
uint16_t* back_border = generate_border_table_back(padding_table, front_border, back_padding_table, *rotated);
for (int y = 0; y < height; ++y)
{
// TODO: compact these structure to increase locality
int const left_padding = padding_table[y];
int const left_border = 0;
int const right_border = 0;
int const left_border = front_border[y];
int const right_border = back_border[y];
int const right_padding = back_padding_table[y];
int const core_pixels = width - left_padding - left_border - right_border - right_padding;
if (core_pixels < 0)
{
ERRLOG << "Too much border at line " << y << endl;
ERRLOG << " width = " << width << endl;
ERRLOG << " left padding = " << left_padding << endl;
ERRLOG << " left border = " << left_border << endl;
ERRLOG << " right border = " << right_border << endl;
ERRLOG << " right padding = " << right_padding << endl;
ERRLOG << " core pixels = " << core_pixels << endl;
abort();
}
if (false)
{
LOG << "left padding = " << left_padding << endl;
@ -569,7 +601,7 @@ Image* rotate(Image const& src, double angle)
// Border
for (int x = 0; x < left_border; ++x, ++buffer_index)
{
buffer[buffer_index] = 70; // TODO: handle border
buffer[buffer_index] = 0; // TODO: handle border
}
Point src_rotated_point(src_rotated_origin.x + (left_padding + left_border) * qdx.x,
@ -608,7 +640,7 @@ Image* rotate(Image const& src, double angle)
// Border
for (int x = 0; x < right_border; ++x, ++buffer_index)
{
buffer[buffer_index] = 80; // TODO: handle border
buffer[buffer_index] = 0; // TODO: handle border
src_rotated_point += qdx;
}