#include "model/processor/strategies/Enhance.h" #include "model/processor/BIP.h" #include #include Enhance::Enhance(const Context context, const QString kernelPath, const QObject *parent = Q_NULLPTR) : IProcessStrategy(context, kernelPath, "Enhance", parent), _kernelFunctor(KernelFunctor(_kernel)) { memset(&_kernelParameters, 0, sizeof (Enhance_t)); _filter = Q_NULLPTR; } void Enhance::cpuProcess(ScenGenOutput_t parameters) { if(parameters.enhance.isUpdated || parameters.enhanceAlgorithm.isUpdated) { _run = false; if(parameters.enhance.value == 0) { return; } float *hpf; int *backgroundRecovery; //sharpen if(parameters.enhanceAlgorithm.value == 0) { auto alpha = 0.25f * (parameters.enhance.value - 1); _kernelParameters.filterWidth = 3; _kernelParameters.filterHeight = 3; hpf = new float[9]; backgroundRecovery = new int[9]; auto factor = 4 / (alpha + 1); int index = 0; hpf[index++] = factor * (alpha / 4); hpf[index++] = factor * ((1 - alpha) / 4); hpf[index++] = factor * (alpha / 4); hpf[index++] = factor * ((1 - alpha) / 4); hpf[index++] = factor * -1; hpf[index++] = factor * ((1 - alpha) / 4); hpf[index++] = factor * (alpha / 4); hpf[index++] = factor * ((1 - alpha) / 4); hpf[index++] = factor * (alpha / 4); index = 0; memset(backgroundRecovery, 0, 9 * sizeof (int)); backgroundRecovery[4] = 1; _run = true; } //log else if(parameters.enhanceAlgorithm.value == 1) { _kernelParameters.filterWidth = 5; _kernelParameters.filterHeight = 5; hpf = new float[25]; backgroundRecovery = new int[25]; auto sigma = 0.5f + 0.25f * (parameters.enhance.value - 1); float sumHg = 0; float h[25]; for(int i = -2; i <= 2; i++) { for(int j = -2; j <= 2; j++) { float temp = expf(-(powf(i, 2) + powf(j, 2)) / 2 / powf(sigma, 2)); h[(i + 2) * 5 + (j + 2)] = (powf(i, 2) + powf(j, 2) - 2 * powf(sigma, 2)) * temp; sumHg += temp; } } for(int i = 0; i < 25 ; i++) { hpf[i] = h[i] / powf(sigma, 4) / sumHg; } memset(backgroundRecovery, 0, 25 * sizeof (int)); backgroundRecovery[12] = 1; _run = true; } if(_run) { auto filterSize = _kernelParameters.filterWidth * _kernelParameters.filterHeight; auto filter = new float[filterSize]; for(int i = 0; i < filterSize ; i++) { filter[i] = backgroundRecovery[i] - hpf[i]; } delete [] hpf; delete [] backgroundRecovery; if(_filter) delete _filter; _filter = _openCLHelper.array2CLBuffer(_CLContext, filterSize * sizeof (float)); BIP::getInstance()->CLQueue.enqueueWriteBuffer(*_filter, CL_TRUE, 0, filterSize * sizeof (float), filter); delete[] filter; } } } void Enhance::finalize() { if(_filter) delete _filter; } Image* Enhance::processKernel(Image *frames, Buffer* scratchPad) { if(_run) { auto format = frames->getImageInfo(); auto width = frames->getImageInfo(); auto height = frames->getImageInfo(); _kernelParameters.width = width; _kernelParameters.height = height; auto imageOutput = new Image2D(_CLContext, CL_MEM_READ_WRITE, ImageFormat(format.image_channel_order, format.image_channel_data_type), width, height); cl::EnqueueArgs eargs(BIP::getInstance()->CLQueue, cl::NDRange(width, height)); _openCLHelper.runKernelFunctor(_kernelFunctor, eargs, *static_cast(frames), *imageOutput, *_filter, _kernelParameters); delete frames; return imageOutput; } else { return frames; } }