2014-12-14 14:26:59 +01:00
|
|
|
#include "utils/ImageResampler.h"
|
2016-06-28 21:53:34 +02:00
|
|
|
#include <utils/Logger.h>
|
2014-12-14 14:26:59 +01:00
|
|
|
|
2016-08-23 07:04:57 +02:00
|
|
|
ImageResampler::ImageResampler()
|
|
|
|
: _horizontalDecimation(1)
|
|
|
|
, _verticalDecimation(1)
|
|
|
|
, _cropLeft(0)
|
|
|
|
, _cropRight(0)
|
|
|
|
, _cropTop(0)
|
|
|
|
, _cropBottom(0)
|
2020-06-28 23:05:32 +02:00
|
|
|
, _videoMode(VideoMode::VIDEO_2D)
|
2014-12-14 14:26:59 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageResampler::~ImageResampler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageResampler::setHorizontalPixelDecimation(int decimator)
|
|
|
|
{
|
|
|
|
_horizontalDecimation = decimator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageResampler::setVerticalPixelDecimation(int decimator)
|
|
|
|
{
|
2016-08-23 07:04:57 +02:00
|
|
|
_verticalDecimation = decimator;
|
2014-12-14 14:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImageResampler::setCropping(int cropLeft, int cropRight, int cropTop, int cropBottom)
|
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
_cropLeft = cropLeft;
|
|
|
|
_cropRight = cropRight;
|
|
|
|
_cropTop = cropTop;
|
2016-08-23 07:04:57 +02:00
|
|
|
_cropBottom = cropBottom;
|
2014-12-14 14:26:59 +01:00
|
|
|
}
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
void ImageResampler::setVideoMode(VideoMode mode)
|
2014-12-14 14:26:59 +01:00
|
|
|
{
|
|
|
|
_videoMode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageResampler::processImage(const uint8_t * data, int width, int height, int lineLength, PixelFormat pixelFormat, Image<ColorRgb> &outputImage) const
|
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
int cropRight = _cropRight;
|
2014-12-14 14:26:59 +01:00
|
|
|
int cropBottom = _cropBottom;
|
|
|
|
|
|
|
|
// handle 3D mode
|
|
|
|
switch (_videoMode)
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
case VideoMode::VIDEO_3DSBS:
|
2019-02-08 09:38:31 +01:00
|
|
|
cropRight = width >> 1;
|
2014-12-14 14:26:59 +01:00
|
|
|
break;
|
2020-06-28 23:05:32 +02:00
|
|
|
case VideoMode::VIDEO_3DTAB:
|
2019-02-08 09:38:31 +01:00
|
|
|
cropBottom = width >> 1;
|
2014-12-14 14:26:59 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate the output size
|
2019-02-08 09:38:31 +01:00
|
|
|
int outputWidth = (width - _cropLeft - cropRight - (_horizontalDecimation >> 1) + _horizontalDecimation - 1) / _horizontalDecimation;
|
|
|
|
int outputHeight = (height - _cropTop - cropBottom - (_verticalDecimation >> 1) + _verticalDecimation - 1) / _verticalDecimation;
|
2020-07-22 16:46:43 +02:00
|
|
|
|
|
|
|
if (outputImage.width() != outputWidth || outputImage.height() != outputHeight)
|
|
|
|
outputImage.resize(outputWidth, outputHeight);
|
2014-12-14 14:26:59 +01:00
|
|
|
|
2019-02-08 09:38:31 +01:00
|
|
|
for (int yDest = 0, ySource = _cropTop + (_verticalDecimation >> 1); yDest < outputHeight; ySource += _verticalDecimation, ++yDest)
|
2014-12-14 14:26:59 +01:00
|
|
|
{
|
2019-02-08 09:38:31 +01:00
|
|
|
for (int xDest = 0, xSource = _cropLeft + (_horizontalDecimation >> 1); xDest < outputWidth; xSource += _horizontalDecimation, ++xDest)
|
2014-12-14 14:26:59 +01:00
|
|
|
{
|
|
|
|
ColorRgb & rgb = outputImage(xDest, yDest);
|
2020-06-28 23:12:22 +02:00
|
|
|
|
2014-12-14 14:26:59 +01:00
|
|
|
switch (pixelFormat)
|
|
|
|
{
|
2020-07-19 16:14:54 +02:00
|
|
|
case PixelFormat::UYVY:
|
2014-12-14 14:26:59 +01:00
|
|
|
{
|
2019-02-08 09:38:31 +01:00
|
|
|
int index = lineLength * ySource + (xSource << 1);
|
2014-12-14 14:26:59 +01:00
|
|
|
uint8_t y = data[index+1];
|
|
|
|
uint8_t u = ((xSource&1) == 0) ? data[index ] : data[index-2];
|
|
|
|
uint8_t v = ((xSource&1) == 0) ? data[index+2] : data[index ];
|
|
|
|
yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
|
|
|
|
}
|
|
|
|
break;
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::YUYV:
|
2014-12-14 14:26:59 +01:00
|
|
|
{
|
2019-02-08 09:38:31 +01:00
|
|
|
int index = lineLength * ySource + (xSource << 1);
|
2014-12-14 14:26:59 +01:00
|
|
|
uint8_t y = data[index];
|
|
|
|
uint8_t u = ((xSource&1) == 0) ? data[index+1] : data[index-1];
|
|
|
|
uint8_t v = ((xSource&1) == 0) ? data[index+3] : data[index+1];
|
|
|
|
yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
|
|
|
|
}
|
|
|
|
break;
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::BGR16:
|
2016-08-23 07:04:57 +02:00
|
|
|
{
|
2019-02-08 09:38:31 +01:00
|
|
|
int index = lineLength * ySource + (xSource << 1);
|
2016-08-23 07:04:57 +02:00
|
|
|
rgb.blue = (data[index] & 0x1f) << 3;
|
|
|
|
rgb.green = (((data[index+1] & 0x7) << 3) | (data[index] & 0xE0) >> 5) << 2;
|
|
|
|
rgb.red = (data[index+1] & 0xF8);
|
|
|
|
}
|
|
|
|
break;
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::BGR24:
|
2016-08-23 07:04:57 +02:00
|
|
|
{
|
2019-02-08 09:38:31 +01:00
|
|
|
int index = lineLength * ySource + (xSource << 1) + xSource;
|
2016-08-23 07:04:57 +02:00
|
|
|
rgb.blue = data[index ];
|
|
|
|
rgb.green = data[index+1];
|
|
|
|
rgb.red = data[index+2];
|
|
|
|
}
|
|
|
|
break;
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::RGB32:
|
2016-08-23 07:04:57 +02:00
|
|
|
{
|
2019-02-08 09:38:31 +01:00
|
|
|
int index = lineLength * ySource + (xSource << 2);
|
2016-08-23 07:04:57 +02:00
|
|
|
rgb.red = data[index ];
|
|
|
|
rgb.green = data[index+1];
|
|
|
|
rgb.blue = data[index+2];
|
|
|
|
}
|
|
|
|
break;
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::BGR32:
|
2016-08-23 07:04:57 +02:00
|
|
|
{
|
2019-02-08 09:38:31 +01:00
|
|
|
int index = lineLength * ySource + (xSource << 2);
|
2016-08-23 07:04:57 +02:00
|
|
|
rgb.blue = data[index ];
|
|
|
|
rgb.green = data[index+1];
|
|
|
|
rgb.red = data[index+2];
|
|
|
|
}
|
|
|
|
break;
|
2020-03-27 23:13:58 +01:00
|
|
|
#ifdef HAVE_JPEG_DECODER
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::MJPEG:
|
2020-07-19 16:14:54 +02:00
|
|
|
break;
|
2019-04-28 19:53:45 +02:00
|
|
|
#endif
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::NO_CHANGE:
|
2016-08-23 07:04:57 +02:00
|
|
|
Error(Logger::getInstance("ImageResampler"), "Invalid pixel format given");
|
2014-12-14 14:26:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t ImageResampler::clamp(int x)
|
|
|
|
{
|
|
|
|
return (x<0) ? 0 : ((x>255) ? 255 : uint8_t(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageResampler::yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t &r, uint8_t &g, uint8_t &b)
|
|
|
|
{
|
|
|
|
// see: http://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion
|
|
|
|
int c = y - 16;
|
|
|
|
int d = u - 128;
|
|
|
|
int e = v - 128;
|
2020-06-28 23:12:22 +02:00
|
|
|
|
2014-12-14 14:26:59 +01:00
|
|
|
r = clamp((298 * c + 409 * e + 128) >> 8);
|
|
|
|
g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
|
|
|
|
b = clamp((298 * c + 516 * d + 128) >> 8);
|
|
|
|
}
|