缩放算法优化步骤详解

这篇具有很好参考价值的文章主要介绍了缩放算法优化步骤详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

添加链接描述

背景

假设数据存放在在unsigned char* m_pData 里面,宽和高分别是:m_nDataWidth m_nDataHeight
给定缩放比例:fXZoom fYZoom,返回缩放后的unsigned char* dataZoom
这里采用最简单的缩放算法即:
根据比例计算原图和缩放后图坐标的对应关系:缩放后图坐标*缩放比例 = 原图坐标

原始代码 未优化

#pragma once
class zoomBlock
{
public:
	zoomBlock() {};
	~zoomBlock();
	void zoomDataSSE128(unsigned char* dataZoom, float  fXZoom, float fYZoom);
	void zoomData(unsigned char* dataZoom, float  fXZoom, float fYZoom);
	void test(float  fXZoom =0.5, float fYZoom=0.5);
	void init(int DataWidth, int DataHeight);
private:
	void computeSrcValues(int* srcValues, size_t size, float zoom, int dataSize);

private:
	unsigned char* m_pData = nullptr;
	float m_fXZoom = 1 ;//x轴缩放比例  m_nXZoom=1时 不缩放
	float m_fYZoom = 1 ;//y轴缩放比例
	int m_nDataWidth = 0;
	int m_nDataHeight = 0;
};

#include "zoomBlock.h"
#include <stdio.h>
#include <iostream>
#include<iomanip>
#define SAFE_DELETE_ARRAY(p) { if( (p) != NULL ) delete[] (p); (p) = NULL; }

zoomBlock::~zoomBlock()
{
	SAFE_DELETE_ARRAY(m_pData);
}
void zoomBlock::init(int DataWidth, int DataHeight)
{
	m_nDataWidth = DataWidth;
	m_nDataHeight = DataHeight;
	m_pData = new unsigned char[m_nDataWidth* m_nDataHeight];
	for (int i = 0; i < m_nDataWidth * m_nDataHeight; ++i)
	{
		m_pData[i] = static_cast<unsigned char>(i);  // Replace this with your data initialization logic
	}
}



void zoomBlock::zoomData(unsigned char* dataZoom, float  fXZoom, float fYZoom)
{
	int nZoomDataWidth = fXZoom * m_nDataWidth;
	int nZoomDataHeight = fYZoom * m_nDataHeight;
	
	for (size_t row = 0; row < nZoomDataHeight; row++)
	{
		for (size_t column = 0; column < nZoomDataWidth; column ++)
		{
			//1
			int srcx = std::min(int(row / fYZoom), m_nDataHeight - 1);
			int srcy = std::min(int(column / fXZoom), m_nDataWidth - 1);

			//2
			int srcPos = srcx * m_nDataHeight + srcy;
			int desPos = row * nZoomDataHeight + column;
			dataZoom[desPos] = m_pData[srcPos];
		}
	}
}


void zoomBlock::test(float  fXZoom, float fYZoom)
{
	init(8,8);
	std::cout << "Values in m_pData:" << std::endl;

	for (int i = 0; i < m_nDataWidth * m_nDataHeight; ++i)
	{
		std::cout << std::setw(4) << static_cast<int>(m_pData[i]) << " ";
		if ((i + 1) % m_nDataWidth == 0) 
		{  // Adjust the value based on your data
			std::cout << std::endl;
		}
	}

	unsigned char* dataZoom = new unsigned char[fXZoom * m_nDataWidth * fYZoom * m_nDataHeight];

	zoomData(dataZoom, fXZoom, fYZoom);

	// Print or inspect the values in m_dataZoom
	int nZoomDataWidth = fXZoom * m_nDataWidth;
	int nZoomDataHeight = fYZoom * m_nDataHeight;

	std::cout << "Values in m_dataZoom:" << std::endl;
	for (int i = 0; i < nZoomDataHeight * nZoomDataWidth; ++i)
	{
		std::cout << std::setw(4)<< static_cast<int>(dataZoom[i]) << " ";
		if ((i + 1) % nZoomDataWidth == 0) {  // Adjust the value based on your data
			std::cout << std::endl;
		}
	}

	SAFE_DELETE_ARRAY(dataZoom);

}

测试代码

int main()
{
	 zoomBlock zoomBlocktest;
	 zoomBlocktest.test(1.5,1.5);
	return 0;
}

缩放算法优化步骤详解,C++,算法
其中函数
·void zoomBlock::zoomData(unsigned char* dataZoom, float fXZoom, float fYZoom)·
没有使用任何加速优化,现在来分析它。

sse128

我们知道sse128可以一次性处理4个int类型,所以我们把最后一层for循环改成,4个坐标的算法,不满4个的单独计算

void zoomBlock::zoomDataSSE128(unsigned char* dataZoom, float  fXZoom, float fYZoom)
{
	int nZoomDataWidth = fXZoom * m_nDataWidth;
	int nZoomDataHeight = fYZoom * m_nDataHeight;

	for (size_t row = 0; row < nZoomDataHeight; row++)
	{
		int remian = nZoomDataWidth % 4;
		for (size_t column = 0; column < nZoomDataWidth - remian; column += 4)
		{
			//第一个坐标
			int srcx = std::min(int(row / fYZoom), m_nDataHeight - 1);
			int srcy = std::min(int(column / fXZoom), m_nDataWidth - 1);
			int srcPos = srcx * m_nDataHeight + srcy;
			int desPos = row * nZoomDataHeight + column;
			dataZoom[desPos] = m_pData[srcPos];

			//第二个坐标
			int srcx1 = std::min(int((row+1) / fYZoom), m_nDataHeight - 1);
			int srcy1 = std::min(int((column+1) / fXZoom), m_nDataWidth - 1);

			int srcPos1 = srcx1 * m_nDataHeight + srcy1;
			int desPos1 = (row+1) * nZoomDataHeight + column+1;
			dataZoom[desPos1] = m_pData[srcPos1];

			//第3个坐标
			// 。。。
			//第4个坐标
			// 。。。
		}
		// Process the remaining elements (if any) without SSE
		for (size_t column = nZoomDataWidth - remian; column < nZoomDataWidth; column++)
		{
			int srcx = std::min(int(row / fYZoom), m_nDataHeight - 1);
			int srcy = std::min(int(column / fXZoom), m_nDataWidth - 1);
			int srcPos = srcx * m_nDataHeight + srcy;
			int desPos = row * nZoomDataHeight + column;
			dataZoom[desPos] = m_pData[srcPos];
		}
	}
}

上面 一次处理四个坐标的代码要改成sse的代码

在最里层的循环里面,每次都要计算 row / fYZoom 和 column / fXZoom,这个实际上可以挪出for循环,计算一次存到数组里

数据坐标desPos和srcPos ,必须放在最内存的循环里

所以我们用calculateSrcIndex函数单独处理 row / fYZoom 和 column / fXZoom,希望达到如下效果:

void calculateSrcIndex(int* srcValues, int size, float zoom,int max)
{
	for (int i = 0; i < size; i++)
	{
		srcValues[i] = std::min(int(i/zoom),max);
	}
}

改成sse:

void calculateSrcIndex(int* srcValues, int size, float zoom,int max)
{
	__m128i mmIndex, mmSrcValue, mmMax;
	mmMax = _mm_set1_epi32(max);
	float zoomReciprocal = 1.0f / zoom;

	int remian = size % 4;
	for (size_t i = 0; i < size - remian; i += 4)
	{
		mmIndex = _mm_set_epi32(i + 3, i + 2, i + 1, i);
		mmSrcValue = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(mmIndex), _mm_set1_ps(zoomReciprocal)));

		// Ensure srcValues are within the valid range [0, max]
		mmSrcValue = _mm_min_epi32(mmSrcValue, mmMax);

		// Store the result to the srcValues array
		_mm_storeu_si128(reinterpret_cast<__m128i*>(&srcValues[i]), mmSrcValue);
	}

	// Process the remaining elements (if any) without SSE
	for (size_t i = size - remian; i < size; i++)
	{
		srcValues[i] = std::min(int(i / zoom), max);
	}
}

解释:
这里主要处理int型数据,为了使用sse加速,要使用__m128i类型来存储4个int

加载int到__m128i:

  1. __m128i _mm_set1_epi32(int i);
    这个指令是使用1个i,来设置__m128i,将__m128i看做4个32位的部分,则每个部分都被赋为i;

  2. __m128i _mm_set_epi32(int i3, int i2,int i1, int i0);
    说明:使用4个int(32bits)变量来设置__m128i变量;
    返回值:如果返回值__m128i,分为r0,r1,r2,r3返回值规则如下:

r0 := i0
r1 := i1
r2 := i2
r3 := i3

  1. __m128i _mm_cvtps_epi32 (__m128 a)
    Converts packed 32-bit integers in a to packed single-precision (32-bit) floating-point elements.

加载float到__m128

  1. __m128 _mm_set1_ps(float w)
    对应于_mm_load1_ps的功能,不需要字节对齐,需要多条指令。(r0 = r1 = r2 = r3 = w)
  2. __m128 _mm_cvtepi32_ps (__m128i a)
    Converts packed 32-bit integers in a to packed single-precision (32-bit) floating-point elements.

float乘法

__m128 dst = _mm_mul_ps (__m128 a, __m128 b)
将a, b中的32位浮点数相乘,结果打包给dst

取最小值

__m128i _mm_min_epi32 (__m128i a, __m128i b)
Compare packed signed 32-bit integers in a and b, and store packed minimum values in dst.
Operation
FOR j := 0 to 3
i := j*32
dst[i+31:i] := MIN(a[i+31:i], b[i+31:i])
ENDFOR

所以代码修改为

	int* srcX = new int[nZoomDataHeight];
	int* srcY = new int[nZoomDataWidth];
	calculateSrcIndex(srcX, nZoomDataHeight, fXZoom , m_nDataHeight - 1);
	calculateSrcIndex(srcY, nZoomDataWidth, fYZoom, m_nDataWidth - 1);

	for (size_t row = 0; row < nZoomDataHeight; row++)
	{
		int remian = nZoomDataWidth % 4;
		for (size_t column = 0; column < nZoomDataWidth - remian; column += 4)
		{
			//第一个坐标
			int srcPos = srcX[row] * m_nDataHeight + srcY[column];
			int desPos = row * nZoomDataHeight + column;
			dataZoom[desPos] = m_pData[srcPos];
			...
			}
	}

然后把坐标的计算转为sse

void zoomBlock::zoomDataSSE128(unsigned char* dataZoom, float  fXZoom, float fYZoom)
{
	int nZoomDataWidth = fXZoom * m_nDataWidth;
	int nZoomDataHeight = fYZoom * m_nDataHeight;

	
	int* srcX = new int[nZoomDataWidth];
	int* srcY = new int[nZoomDataHeight];
	
	calculateSrcIndex(srcX, nZoomDataWidth, fXZoom, m_nDataWidth - 1);
	calculateSrcIndex(srcY, nZoomDataHeight, fYZoom, m_nDataHeight - 1);

	for (size_t y = 0; y < nZoomDataHeight; y++)
	{
		int remian = nZoomDataWidth % 4;
		for (size_t x = 0; x < nZoomDataWidth - remian; x += 4)
		{
			__m128i mmsrcX = _mm_set_epi32(srcX[x + 3], srcX[x + 2], srcX[x+1], srcX[x]);
			__m128i srcPosIndices = _mm_add_epi32(
				_mm_set1_epi32(srcY[y] * m_nDataWidth),
				mmsrcX);

			__m128i desPosIndices = _mm_add_epi32(
				_mm_set1_epi32(y * nZoomDataWidth),
				_mm_set_epi32(x + 3, x + 2, x + 1, x)
				);
			dataZoom[desPosIndices.m128i_i32[0]] = m_pData[srcPosIndices.m128i_i32[0]];
			dataZoom[desPosIndices.m128i_i32[1]] = m_pData[srcPosIndices.m128i_i32[1]];
			dataZoom[desPosIndices.m128i_i32[2]] = m_pData[srcPosIndices.m128i_i32[2]];
			dataZoom[desPosIndices.m128i_i32[3]] = m_pData[srcPosIndices.m128i_i32[3]];
			/*cout << "srcPosIndices: " << srcPosIndices.m128i_i32[0] << " , desPosIndices : " << desPosIndices.m128i_i32[0] << endl;
				cout << "srcPosIndices: " << srcPosIndices.m128i_i32[1] << " , desPosIndices : " << desPosIndices.m128i_i32[1] << endl;
				cout << "srcPosIndices: " << srcPosIndices.m128i_i32[2] << " , desPosIndices : " << desPosIndices.m128i_i32[2] << endl;
				cout << "srcPosIndices: " << srcPosIndices.m128i_i32[3] << " , desPosIndices : " << desPosIndices.m128i_i32[3] << endl;*/
		}
		// Process the remaining elements (if any) without SSE
		for (size_t x = nZoomDataWidth - remian; x < nZoomDataWidth; x++)
		{
			int srcy = std::min(int(y / fYZoom), m_nDataHeight - 1);
			int srcx = std::min(int(x / fXZoom), m_nDataWidth - 1);
			int srcPos = srcy * m_nDataHeight + srcx;
			int desPos = y * nZoomDataHeight + x;
			dataZoom[desPos] = m_pData[srcPos];
		}
	}
	delete[] srcX;
	delete[] srcY;
}

完整的代码

 #pragma once
class zoomBlock
{
public:
	zoomBlock() {};
	~zoomBlock();
	void zoomDataSSE128(unsigned char* dataZoom, float  fXZoom, float fYZoom);
	void zoomData(unsigned char* dataZoom, float  fXZoom, float fYZoom);
	void test(float  fXZoom =0.5, float fYZoom=0.5);
	void init(int DataWidth, int DataHeight);
private:
	inline void calculateSrcIndex(int* srcValues, int size, float zoom, int max);

private:
	unsigned char* m_pData = nullptr;
	float m_fXZoom = 1 ;//x轴缩放比例  m_nXZoom=1时 不缩放
	float m_fYZoom = 1 ;//y轴缩放比例
	int m_nDataWidth = 0;
	int m_nDataHeight = 0;
};

#include "zoomBlock.h"
#include <stdio.h>
#include <iostream>
#include<iomanip>
#include<immintrin.h> 
using namespace std;
#define SAFE_DELETE_ARRAY(p) { if( (p) != NULL ) delete[] (p); (p) = NULL; }

zoomBlock::~zoomBlock()
{
	SAFE_DELETE_ARRAY(m_pData);
}
void zoomBlock::init(int DataWidth, int DataHeight)
{
	m_nDataWidth = DataWidth;
	m_nDataHeight = DataHeight;
	m_pData = new unsigned char[m_nDataWidth* m_nDataHeight];
	for (int i = 0; i < m_nDataWidth * m_nDataHeight; ++i)
	{
		m_pData[i] = static_cast<unsigned char>(i);  // Replace this with your data initialization logic
	}
}


void zoomBlock::zoomData(unsigned char* dataZoom, float  fXZoom, float fYZoom)
{
	int nZoomDataWidth = fXZoom * m_nDataWidth;
	int nZoomDataHeight = fYZoom * m_nDataHeight;
	
	for (size_t y = 0; y < nZoomDataHeight; y++)
	{
		for (size_t x = 0; x < nZoomDataWidth; x ++)
		{
			//1
			int srcy = std::min(int(y / fYZoom), m_nDataHeight - 1);
			int srcx = std::min(int(x / fXZoom), m_nDataWidth - 1);

			//2
			int srcPos = srcy * m_nDataWidth + srcx;
			int desPos = y * nZoomDataWidth + x;
			dataZoom[desPos] = m_pData[srcPos];
		}
	}
}

inline void zoomBlock::calculateSrcIndex(int* srcValues, int size, float zoom,int max)
{
	__m128i mmIndex, mmSrcValue, mmMax;
	mmMax = _mm_set1_epi32(max);
	float zoomReciprocal = 1.0f / zoom;

	int remian = size % 4;
	for (size_t i = 0; i < size - remian; i += 4)
	{
		mmIndex = _mm_set_epi32(i + 3, i + 2, i + 1, i);
		mmSrcValue = _mm_cvttps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(mmIndex), _mm_set1_ps(zoomReciprocal)));

		// Ensure srcValues are within the valid range [0, max]
		mmSrcValue = _mm_min_epi32(mmSrcValue, mmMax);

		// Store the result to the srcValues array
		_mm_storeu_si128(reinterpret_cast<__m128i*>(&srcValues[i]), mmSrcValue);
	}

	// Process the remaining elements (if any) without SSE
	for (size_t i = size - remian; i < size; i++)
	{
		srcValues[i] = std::min(int(i / zoom), max);
	}
}

void zoomBlock::zoomDataSSE128(unsigned char* dataZoom, float  fXZoom, float fYZoom)
{
	int nZoomDataWidth = fXZoom * m_nDataWidth;
	int nZoomDataHeight = fYZoom * m_nDataHeight;

	
	int* srcX = new int[nZoomDataWidth];
	int* srcY = new int[nZoomDataHeight];
	
	calculateSrcIndex(srcX, nZoomDataWidth, fXZoom, m_nDataWidth - 1);
	calculateSrcIndex(srcY, nZoomDataHeight, fYZoom, m_nDataHeight - 1);

	for (size_t y = 0; y < nZoomDataHeight; y++)
	{
		int remian = nZoomDataWidth % 4;
		for (size_t x = 0; x < nZoomDataWidth - remian; x += 4)
		{
			/*int srcPos = srcx * m_nDataHeight + srcy;
			int desPos = row * nZoomDataHeight + column;*/
			//dataZoom[desPos] = m_pData[srcPos];

			//__m128i mmsrcY = _mm_loadu_si128((__m128i*)(srcY));
			__m128i mmsrcX = _mm_set_epi32(srcX[x + 3], srcX[x + 2], srcX[x+1], srcX[x]);
			__m128i srcPosIndices = _mm_add_epi32(
				_mm_set1_epi32(srcY[y] * m_nDataWidth),
				mmsrcX);

			__m128i desPosIndices = _mm_add_epi32(
				_mm_set1_epi32(y * nZoomDataWidth),
				_mm_set_epi32(x + 3, x + 2, x + 1, x)
				);
			dataZoom[desPosIndices.m128i_i32[0]] = m_pData[srcPosIndices.m128i_i32[0]];
			dataZoom[desPosIndices.m128i_i32[1]] = m_pData[srcPosIndices.m128i_i32[1]];
			dataZoom[desPosIndices.m128i_i32[2]] = m_pData[srcPosIndices.m128i_i32[2]];
			dataZoom[desPosIndices.m128i_i32[3]] = m_pData[srcPosIndices.m128i_i32[3]];
			/*cout << "srcPosIndices: " << srcPosIndices.m128i_i32[0] << " , desPosIndices : " << desPosIndices.m128i_i32[0] << endl;
				cout << "srcPosIndices: " << srcPosIndices.m128i_i32[1] << " , desPosIndices : " << desPosIndices.m128i_i32[1] << endl;
				cout << "srcPosIndices: " << srcPosIndices.m128i_i32[2] << " , desPosIndices : " << desPosIndices.m128i_i32[2] << endl;
				cout << "srcPosIndices: " << srcPosIndices.m128i_i32[3] << " , desPosIndices : " << desPosIndices.m128i_i32[3] << endl;*/
		}
		// Process the remaining elements (if any) without SSE
		for (size_t x = nZoomDataWidth - remian; x < nZoomDataWidth; x++)
		{
			int srcy = std::min(int(y / fYZoom), m_nDataHeight - 1);
			int srcx = std::min(int(x / fXZoom), m_nDataWidth - 1);
			int srcPos = srcy * m_nDataHeight + srcx;
			int desPos = y * nZoomDataHeight + x;
			dataZoom[desPos] = m_pData[srcPos];
		}
	}
	delete[] srcX;
	delete[] srcY;
}


void zoomBlock::test(float  fXZoom, float fYZoom)
{
	init(8,4);
	std::cout << "Values in m_pData:" << std::endl;

	for (int i = 0; i < m_nDataWidth * m_nDataHeight; ++i)
	{
		std::cout << std::setw(4) << static_cast<int>(m_pData[i]) << " ";
		if ((i + 1) % m_nDataWidth == 0) 
		{  // Adjust the value based on your data
			std::cout << std::endl;
		}
	}

	
	int nZoomDataWidth = fXZoom * m_nDataWidth;
	int nZoomDataHeight = fYZoom * m_nDataHeight;
	unsigned char* dataZoom = new unsigned char[nZoomDataWidth * nZoomDataHeight];
	zoomDataSSE128(dataZoom, fXZoom, fYZoom);
	//zoomData(dataZoom, fXZoom, fYZoom);
	// Print or inspect the values in m_dataZoom

	std::cout << "Values in m_dataZoom:" << std::endl;
	for (int i = 0; i < nZoomDataHeight * nZoomDataWidth; ++i)
	{
		std::cout << std::setw(4)<< static_cast<int>(dataZoom[i]) << " ";
		if ((i + 1) % nZoomDataWidth == 0) {  // Adjust the value based on your data
			std::cout << std::endl;
		}
	}

	SAFE_DELETE_ARRAY(dataZoom);

}

int main()
{
	 zoomBlock zoomBlocktest;
	 zoomBlocktest.test(2,1);
	return 0;
}

缩放算法优化步骤详解,C++,算法文章来源地址https://www.toymoban.com/news/detail-837941.html

AVX 256

inline void zoomBlock::calculateSrcIndex256(int* srcValues, int size, float zoom, int max)
{
	__m256i ymmIndex, ymmSrcValue, ymmMax;
	ymmMax = _mm256_set1_epi32(max);
	float zoomReciprocal = 1.0f / zoom;

	int remian = size % 8;
	for (size_t i = 0; i < size - remian; i += 8)
	{
		ymmIndex = _mm256_set_epi32(i + 7, i + 6, i + 5, i + 4, i + 3, i + 2, i + 1, i);
		ymmSrcValue = _mm256_cvtps_epi32(_mm256_mul_ps(_mm256_cvtepi32_ps(ymmIndex), _mm256_set1_ps(zoomReciprocal)));

		// Ensure srcValues are within the valid range [0, max]
		ymmSrcValue = _mm256_min_epi32(ymmSrcValue, ymmMax);

		// Store the result to the srcValues array
		_mm256_storeu_si256(reinterpret_cast<__m256i*>(&srcValues[i]), ymmSrcValue);
	}

	// Process the remaining elements (if any) without AVX2
	for (size_t i = size - remian; i < size; i++)
	{
		srcValues[i] = std::min(int(i / zoom), max);
	}
}
void zoomBlock::zoomDataAVX2(unsigned char* dataZoom, float fXZoom, float fYZoom)
{
	int nZoomDataWidth = fXZoom * m_nDataWidth;
	int nZoomDataHeight = fYZoom * m_nDataHeight;

	int* srcX = new int[nZoomDataWidth];
	int* srcY = new int[nZoomDataHeight];

	calculateSrcIndex(srcX, nZoomDataWidth, fXZoom, m_nDataWidth - 1);
	calculateSrcIndex(srcY, nZoomDataHeight, fYZoom, m_nDataHeight - 1);

	for (size_t y = 0; y < nZoomDataHeight; y++)
	{
		int remian = nZoomDataWidth % 8;
		for (size_t x = 0; x < nZoomDataWidth - remian; x += 8)
		{
			__m256i ymmSrcX = _mm256_set_epi32(srcX[x + 7], srcX[x + 6], srcX[x + 5], srcX[x + 4],
				srcX[x + 3], srcX[x + 2], srcX[x + 1], srcX[x]);
			__m256i srcPosIndices = _mm256_add_epi32(
				_mm256_set1_epi32(srcY[y] * m_nDataWidth),
				ymmSrcX);

			__m256i desPosIndices = _mm256_add_epi32(
				_mm256_set1_epi32(y * nZoomDataWidth),
				_mm256_set_epi32(x + 7, x + 6, x + 5, x + 4, x + 3, x + 2, x + 1, x));

			dataZoom[desPosIndices.m256i_i32[0]] = m_pData[srcPosIndices.m256i_i32[0]];
			dataZoom[desPosIndices.m256i_i32[1]] = m_pData[srcPosIndices.m256i_i32[1]];
			dataZoom[desPosIndices.m256i_i32[2]] = m_pData[srcPosIndices.m256i_i32[2]];
			dataZoom[desPosIndices.m256i_i32[3]] = m_pData[srcPosIndices.m256i_i32[3]];
			dataZoom[desPosIndices.m256i_i32[4]] = m_pData[srcPosIndices.m256i_i32[4]];
			dataZoom[desPosIndices.m256i_i32[5]] = m_pData[srcPosIndices.m256i_i32[5]];
			dataZoom[desPosIndices.m256i_i32[6]] = m_pData[srcPosIndices.m256i_i32[6]];
			dataZoom[desPosIndices.m256i_i32[7]] = m_pData[srcPosIndices.m256i_i32[7]];
		}

		// Process the remaining elements (if any) without AVX2
		for (size_t x = nZoomDataWidth - remian; x < nZoomDataWidth; x++)
		{
			int srcy = std::min(int(y / fYZoom), m_nDataHeight - 1);
			int srcx = std::min(int(x / fXZoom), m_nDataWidth - 1);
			int srcPos = srcy * m_nDataWidth + srcx;
			int desPos = y * nZoomDataWidth + x;
			dataZoom[desPos] = m_pData[srcPos];
		}
	}
	delete[] srcX;
	delete[] srcY;
}

到了这里,关于缩放算法优化步骤详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • RRT与RRT*算法具体步骤与程序详解(python)

    提示:前面写了A*、Dijkstra算法 RRT和RRT*的区别: RRT的中文名为快速随机探索树,它的原理很简单,实际上就是维护一棵路径树:从起点开始,在空间中随机采样,并找到路径树上与采样点最接近且能与它无障碍地连接的点,连接这个点与采样点,将采样点加入路径树,直至终

    2024年03月22日
    浏览(34)
  • 排序算法——冒泡排序详解及优化

    对于一个排序算法,假设两个相同的元素Ai和Aj· 在排序前这两个元素满足条件ij,即Ai在Aj之前· 在排序后Ai仍在Aj之前,则称为排序算法为稳定排序· 否则称这个算法为不稳定排序 稳定性的说明 排序的稳定性并不影响排序算法的效率,稳定性只对类/结构体类型数据有影响 这

    2024年02月06日
    浏览(40)
  • web 性能优化详解(Lighthouse工具、优化方式、强缓存和协商缓存、代码优化、算法优化)

    优化性能概念宽泛,可以从信号、系统、计算机原理、操作系统、网络通信、DNS解析、负载均衡、页面渲染。只要结合一个实际例子讲述清楚即可。 Web 性能是客观的衡量标准,是用户对加载时间和运行时的直观体验。 Web 性能指页面加载到可交互和可响应所消耗的时间,以

    2024年02月07日
    浏览(45)
  • 详解图的最短路径算法(BFS、Dijkstra、Floyd)(附上图解步骤)

    最短路径分为两种: (1)单源路径:从某顶点出发,到其他全部顶点的最短路径 (2)顶点间的最短路径:任意两个顶点之间的最短路径 最短路径的结果主要有两个方面: (1)顶点之间最短路径的长度 (2)从源顶点到目标顶点的路径 只有边权为 1 时才能用BFS求最短路。

    2024年02月05日
    浏览(54)
  • Adam优化器算法详解及代码实现

    在介绍Adam算法之前,先谈谈Adam中两个关键的算法: 学习率调整(RMSprop 算法) 与 梯度估计修正 。 学习率是神经网络优化时的重要超参数。在标准的梯度下降法中,每个参数在每次迭代时都使用相同的学习率,但是学习率如果过大就不会收敛,如果过小则收敛速度太慢。

    2024年02月02日
    浏览(46)
  • 详解视频美颜SDK:算法优化与性能提升

    众所周知,视频美颜SDK的算法优化和性能提升至关重要。下文小编将与大家深度探讨视频美颜SDK的算法原理,以及近期的性能优化措施。 一、常见用法 视频美颜SDK对人脸进行识别,并附加适当的美颜效果。例如: 1.识别、关键点 2.肤色调整 3.磨皮处理 4.瘦脸大眼 二、性能提

    2024年02月03日
    浏览(41)
  • 智能算法之浣熊优化算法(COA),原理公式详解,附matlab代码

    长鼻浣熊优化算法( Coati Optimization Algorithm , COA )是一种新型元启发式优化算法,该算法是受浣熊狩猎行为启发而提出的,具有进化能力强、搜索速度快、寻优能力强的特点。该成果于 2023 年发表在知名 SCI 期刊 Knowledge-Based Systems 上。目前谷歌学术上查询被引 94 次。 先说一

    2024年02月02日
    浏览(58)
  • 【排序算法】详解冒泡排序及其多种优化&稳定性分析

    冒泡排序(Bubble Sort) 就是从序列中的 第一个元素 开始,依次对 相邻的两个元素 进行比较,如果前一个元素 大于 后一个元素则交换它们的位置。如果前一个元素 小于或等于 后一个元素,则不交换它们;然后每一轮目前的元素中最大的或最小的排到最上面,就像水中的泡泡冒

    2024年02月07日
    浏览(37)
  • 一文速学数模-最优化算法(二)梯度下降算法一文详解+Python代码

    目录 前言 一、梯度下降法简述 二、梯度下降算法原理理解 1.梯度 2.梯度定义

    2023年04月24日
    浏览(46)
  • Unity3D PVP游戏位置同步算法优化详解

    在Unity3D中,PVP(Player versus Player)游戏的位置同步是一项重要的技术,它决定了游戏中玩家之间的互动体验。本文将详细介绍Unity3D PVP游戏位置同步算法的优化方法,并给出相应的技术详解和代码实现。 对啦!这里有个游戏开发交流小组里面聚集了一帮热爱学习游戏的零基础

    2024年01月16日
    浏览(63)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包