2013-08-21 22:44:17 +02:00
|
|
|
|
|
|
|
// Local-Hyperion includes
|
2013-08-21 16:25:27 +02:00
|
|
|
#include "BlackBorderDetector.h"
|
|
|
|
|
2013-08-23 07:08:44 +02:00
|
|
|
using namespace hyperion;
|
|
|
|
|
2013-08-21 16:25:27 +02:00
|
|
|
BlackBorderDetector::BlackBorderDetector()
|
|
|
|
{
|
2013-08-21 16:52:03 +02:00
|
|
|
// empty
|
2013-08-21 16:25:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BlackBorder BlackBorderDetector::process(const RgbImage& image)
|
|
|
|
{
|
2013-10-27 09:25:02 +01:00
|
|
|
// only test the topleft third of the image
|
|
|
|
int width = image.width() /3;
|
|
|
|
int height = image.height() / 3;
|
|
|
|
int maxSize = std::max(width, height);
|
2013-08-21 16:25:27 +02:00
|
|
|
|
2013-10-27 09:25:02 +01:00
|
|
|
int firstNonBlackXPixelIndex = -1;
|
|
|
|
int firstNonBlackYPixelIndex = -1;
|
|
|
|
|
|
|
|
// find some pixel of the image
|
|
|
|
for (int i = 0; i < maxSize; ++i)
|
2013-08-21 16:25:27 +02:00
|
|
|
{
|
2013-10-27 09:25:02 +01:00
|
|
|
int x = std::min(i, width);
|
|
|
|
int y = std::min(i, height);
|
|
|
|
|
|
|
|
const RgbColor& color = image(x, y);
|
2013-08-21 16:25:27 +02:00
|
|
|
if (!isBlack(color))
|
|
|
|
{
|
2013-10-27 09:25:02 +01:00
|
|
|
firstNonBlackXPixelIndex = x;
|
|
|
|
firstNonBlackYPixelIndex = y;
|
2013-08-21 16:25:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-27 09:25:02 +01:00
|
|
|
|
|
|
|
// expand image to the left
|
|
|
|
for(; firstNonBlackXPixelIndex > 0; --firstNonBlackXPixelIndex)
|
2013-08-21 16:25:27 +02:00
|
|
|
{
|
2013-10-27 09:25:02 +01:00
|
|
|
const RgbColor& color = image(firstNonBlackXPixelIndex-1, firstNonBlackYPixelIndex);
|
|
|
|
if (isBlack(color))
|
2013-08-21 16:25:27 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 09:25:02 +01:00
|
|
|
// expand image to the top
|
|
|
|
for(; firstNonBlackYPixelIndex > 0; --firstNonBlackYPixelIndex)
|
2013-08-21 16:25:27 +02:00
|
|
|
{
|
2013-10-27 09:25:02 +01:00
|
|
|
const RgbColor& color = image(firstNonBlackXPixelIndex, firstNonBlackYPixelIndex-1);
|
|
|
|
if (isBlack(color))
|
2013-08-21 16:25:27 +02:00
|
|
|
{
|
2013-10-27 09:25:02 +01:00
|
|
|
break;
|
2013-08-21 16:25:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-27 09:25:02 +01:00
|
|
|
// Construct result
|
|
|
|
BlackBorder detectedBorder;
|
|
|
|
detectedBorder.unknown = firstNonBlackXPixelIndex == -1 || firstNonBlackYPixelIndex == -1;
|
|
|
|
detectedBorder.horizontalSize = firstNonBlackYPixelIndex;
|
|
|
|
detectedBorder.verticalSize = firstNonBlackXPixelIndex;
|
2013-08-21 16:25:27 +02:00
|
|
|
return detectedBorder;
|
|
|
|
}
|