画像処理ライブラリ (Intel Performance Primitives、ImageMagick、Windows など) を既に使用している場合は、既にこれを行っている既存の関数を探してください。作成者が苦労して MMX または SSE 命令を使用した場合、ライブラリの実装はより高速になる可能性があります。
それができない場合は、Ed S. のソリューションと同様のソリューションを使用して独自のロールを作成できます。これがスケッチです。memset 呼び出しの数を半分に削減するなど、追加のマイクロ最適化が可能です。
unsigned char* padWithBlack(const unsigned char* in,
int inWidth, int inHeight,
int thickness)
{
unsigned char* out = malloc((inWidth + thickness * 2) *
(inHeight + thickness * 2) * 3);
// top border
int topOrBottomOutBytes = (inWidth + thickness * 2) * thickness * 3;
memset(out, 0, topOrBottomOutBytes);
// middle section
unsigned char* inRow = in;
unsigned char* outRow = out + topOrBottomOutBytes;
for (int inY = 0; inY < inHeight; inY++) {
// left border
memset(outRow, 0, thickness * 3);
outRow += thickness * 3;
// center section
memcpy(outRow, inRow, inWidth * 3);
outRow += inWidth * 3;
inRow += inWidth * 3;
// right border
memset(outRow, 0, thickness * 3);
outRow += thickness * 3;
}
// bottom border
memset(out, 0, topOrBottomOutBytes);
}