I wrote a small function to rescale a image to a specific size without cropping it (by adding black borders to fit it) and it works, yet it is slow specially on high resolutions. Please take a look and tell me how can I increase the efficiency of the function. Or if there s a better code that I can use to achieve the same result.
static int ReScale(char* srcBuffer, int srcLen, int srcStart, int srcStride, int srcHeight, VideoInfo::ePixelFormat srcPixelFormat,
char* dstBuffer, int dstLen, int dstStart, int dstStride, int dstHeight, VideoInfo::ePixelFormat dstPixelFormat, bool reverseCopy)
{
int srcPixelDepth = VideoInfo::GetPixelFormatSize(srcPixelFormat);
int srcWidth = static_cast<int>(floor(srcStride / static_cast<float>(srcPixelDepth)));
int dstPixelDepth = VideoInfo::GetPixelFormatSize(dstPixelFormat);
int dstWidth = static_cast<int>(floor(dstStride / static_cast<float>(dstPixelDepth)));
float resizeRatio = min(dstWidth / static_cast<float>(srcWidth), dstHeight / static_cast<float>(srcHeight));
int dstXOffset = static_cast<int>((dstWidth - (resizeRatio * srcWidth)) / 2.f);
int dstYOffset = static_cast<int>((dstHeight - (resizeRatio * srcHeight)) / 2.f);
ZeroMemory(dstBuffer + dstStart, dstLen);
srcBuffer += srcStart;
dstBuffer += dstStart;
dstWidth -= 2 * dstXOffset;
dstHeight -= 2 * dstYOffset;
int dstPixelOffset = 0;
int srcPixelOffset = 0;
for (int y = 0; y < dstHeight; y++)
{
dstPixelOffset = dstXOffset * dstPixelDepth + (y + dstYOffset) * dstStride;
for (int x = 0; x < dstWidth; x++)
{
srcPixelOffset = static_cast<int>(min(y / resizeRatio, srcHeight));
if (reverseCopy)
{
srcPixelOffset = srcHeight - (srcPixelOffset + 1);
}
srcPixelOffset = static_cast<int>(min(x / resizeRatio, srcWidth)) * srcPixelDepth + srcPixelOffset * srcStride;
if (srcPixelOffset + srcPixelDepth < srcLen && dstPixelOffset + dstPixelDepth < dstLen)
{
memcpy(dstBuffer + dstPixelOffset, srcBuffer + srcPixelOffset, srcPixelDepth);
}
dstPixelOffset += dstPixelDepth;
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire