KDEAmbi/BlackBorderDetector.cpp

62 lines
1.7 KiB
C++

#include "BlackBorderDetector.h"
#include <QDebug>
BlackBorderDetector::BlackBorderDetector(double threshold)
{
_blackThreshold = static_cast<uint8_t>(255.0 * threshold);
}
bool BlackBorderDetector::isBlack(const QColor &color) const
{
return color.red() < _blackThreshold && color.green() < _blackThreshold && color.blue() < _blackThreshold;
}
BlackBorder BlackBorderDetector::process(const QImage &image)
{
BlackBorder border;
if (image.isNull()) {
return border;
}
int width = image.width();
int height = image.height();
int width33 = width / 3;
int height33 = height / 3;
int width66 = width33 * 2;
int height66 = height33 * 2;
int xCenter = width / 2;
int yCenter = height / 2;
int firstNonBlackX = -1;
int firstNonBlackY = -1;
// Find the first non-black pixel from the left edge
for (int x = 0; x < width33; ++x) {
if (!isBlack(image.pixelColor(x, height33)) ||
!isBlack(image.pixelColor(x, height66)) ||
!isBlack(image.pixelColor(width - 1 - x, yCenter))) {
firstNonBlackX = x;
break;
}
}
// Find the first non-black pixel from the top edge
for (int y = 0; y < height33; ++y) {
if (!isBlack(image.pixelColor(width33, y)) ||
!isBlack(image.pixelColor(width66, y)) ||
!isBlack(image.pixelColor(xCenter, height - 1 - y))) {
firstNonBlackY = y;
break;
}
}
if (firstNonBlackX != -1 && firstNonBlackY != -1) {
border.unknown = false;
border.verticalSize = firstNonBlackX;
border.horizontalSize = firstNonBlackY;
}
return border;
}