Get rid of unnecessary computation in ImageResampler (#947)

This commit is contained in:
Murat Seker 2020-08-08 12:57:15 +02:00 committed by GitHub
parent be329fb7bb
commit f3f07e0c82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 6 deletions

View File

@ -1,4 +1,5 @@
#include "utils/ImageResampler.h" #include "utils/ImageResampler.h"
#include <utils/Logger.h> #include <utils/Logger.h>
ImageResampler::ImageResampler() ImageResampler::ImageResampler()
@ -65,6 +66,8 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
for (int yDest = 0, ySource = _cropTop + (_verticalDecimation >> 1); yDest < outputHeight; ySource += _verticalDecimation, ++yDest) for (int yDest = 0, ySource = _cropTop + (_verticalDecimation >> 1); yDest < outputHeight; ySource += _verticalDecimation, ++yDest)
{ {
int yOffset = lineLength * ySource;
for (int xDest = 0, xSource = _cropLeft + (_horizontalDecimation >> 1); xDest < outputWidth; xSource += _horizontalDecimation, ++xDest) for (int xDest = 0, xSource = _cropLeft + (_horizontalDecimation >> 1); xDest < outputWidth; xSource += _horizontalDecimation, ++xDest)
{ {
ColorRgb & rgb = outputImage(xDest, yDest); ColorRgb & rgb = outputImage(xDest, yDest);
@ -73,7 +76,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
{ {
case PixelFormat::UYVY: case PixelFormat::UYVY:
{ {
int index = lineLength * ySource + (xSource << 1); int index = yOffset + (xSource << 1);
uint8_t y = data[index+1]; uint8_t y = data[index+1];
uint8_t u = ((xSource&1) == 0) ? data[index ] : data[index-2]; uint8_t u = ((xSource&1) == 0) ? data[index ] : data[index-2];
uint8_t v = ((xSource&1) == 0) ? data[index+2] : data[index ]; uint8_t v = ((xSource&1) == 0) ? data[index+2] : data[index ];
@ -82,7 +85,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
break; break;
case PixelFormat::YUYV: case PixelFormat::YUYV:
{ {
int index = lineLength * ySource + (xSource << 1); int index = yOffset + (xSource << 1);
uint8_t y = data[index]; uint8_t y = data[index];
uint8_t u = ((xSource&1) == 0) ? data[index+1] : data[index-1]; uint8_t u = ((xSource&1) == 0) ? data[index+1] : data[index-1];
uint8_t v = ((xSource&1) == 0) ? data[index+3] : data[index+1]; uint8_t v = ((xSource&1) == 0) ? data[index+3] : data[index+1];
@ -91,7 +94,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
break; break;
case PixelFormat::BGR16: case PixelFormat::BGR16:
{ {
int index = lineLength * ySource + (xSource << 1); int index = yOffset + (xSource << 1);
rgb.blue = (data[index] & 0x1f) << 3; rgb.blue = (data[index] & 0x1f) << 3;
rgb.green = (((data[index+1] & 0x7) << 3) | (data[index] & 0xE0) >> 5) << 2; rgb.green = (((data[index+1] & 0x7) << 3) | (data[index] & 0xE0) >> 5) << 2;
rgb.red = (data[index+1] & 0xF8); rgb.red = (data[index+1] & 0xF8);
@ -99,7 +102,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
break; break;
case PixelFormat::BGR24: case PixelFormat::BGR24:
{ {
int index = lineLength * ySource + (xSource << 1) + xSource; int index = yOffset + (xSource << 1) + xSource;
rgb.blue = data[index ]; rgb.blue = data[index ];
rgb.green = data[index+1]; rgb.green = data[index+1];
rgb.red = data[index+2]; rgb.red = data[index+2];
@ -107,7 +110,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
break; break;
case PixelFormat::RGB32: case PixelFormat::RGB32:
{ {
int index = lineLength * ySource + (xSource << 2); int index = yOffset + (xSource << 2);
rgb.red = data[index ]; rgb.red = data[index ];
rgb.green = data[index+1]; rgb.green = data[index+1];
rgb.blue = data[index+2]; rgb.blue = data[index+2];
@ -115,7 +118,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
break; break;
case PixelFormat::BGR32: case PixelFormat::BGR32:
{ {
int index = lineLength * ySource + (xSource << 2); int index = yOffset + (xSource << 2);
rgb.blue = data[index ]; rgb.blue = data[index ];
rgb.green = data[index+1]; rgb.green = data[index+1];
rgb.red = data[index+2]; rgb.red = data[index+2];