c++现有c阵列的焊盘侧
c++ Pad sides of existing C array
我有一个C数组,即<type>* myarray
,它在逻辑上是<type>* myarray[#][#]
。它是来自RGBA图像的数据。我想通过将内容移动offsetx, offsety
并用零填充额外的空间来放大"图像"。
01 02 03 04
05 06 07 08
09 10 11 12
偏移1, -1
产生
00 01 02 03 04
00 05 06 07 08
00 09 10 11 12
00 00 00 00 00
显然有一种有效的方法可以做到这一点,但我被难住了。
Edit01:我正试图做一些类似的事情
/**
* @brief Shifts the src image by offset(x,y) into the dst_img.
* @param src_img Pointer to image data with pixels of arg_type. I.e., an
* RGBA8888 image would be arg_type = Uint8.
* @param dst_img Pointer to output image data. **Must** be sized to fit the source image
* plus the magnitude of the offsets. I.e. a 2x2 source with 1,-1 offsets requires a
* 3x3 dst.
* @param src_h Height of the source
* @param src_w Width of the source
* @param src_channels Number of channels per pixel. I.e. RGBA8888 -> 4, HSV32f32f32f -> 3.
* @param offset_y Vert. offset
* @param offset_x Horiz. offset
*/
template <typename arg_type>
void shift_image(arg_type* src_img, arg_type* dst_img,
Uint32 src_h, Uint32 src_w, Uint32 src_channels,
int offset_y, int offset_x) {
Uint32 src_pixels = (src_h * src_w * src_channels);
std::valarray<arg_type> srcval = std::valarray<arg_type>(src_img,
sizeof(arg_type) * src_pixels);
Uint32 dst_pixels = (src_h + abs(offset_y)) * (src_w + abs(offset_x)) * src_channels;
std::valarray<arg_type> dstval = std::valarray<arg_type>((arg_type)0,
sizeof(arg_type) * dst_pixels);
std::slice srccolslice = std::slice(0, src_w * src_channels, 1);
std::slice dstcolslice;
if (offset_y < 0 ) { // Top Row -> Top Row
for (Uint32 y = 0; y < src_h; y++) { // Left Col -> Left Col
if (offset_x < 0) {
dstcolslice = std::slice(0, src_w * src_channels, 1);
} else { // Right Col -> Right Col
dstcolslice = std::slice(offset_x, (src_w + offset_x) * src_channels, 1);
}
dstval[y][dstcolslice] = srcval[y][srccolslice];
}
} else { // Bot Row -> Bot Row
for (Uint32 y = 0; y < src_h; y++) { // Left Col -> Left Col
if (offset_x < 0) {
dstcolslice = std::slice(0, src_w, 1);
} else { // Right Col -> Right Col
dstcolslice = std::slice(offset_x, (src_w + offset_x) * src_channels, 1);
}
dstval[y + offset_y][dstcolslice] = srcval[y][srccolslice];
}
}
}
如果我没有正确理解操作,我很抱歉,但
1) 代码中的第二个切片参数不应该是src_w * src_channels
而不是src_w
吗?(如果其他条款中有子条款)。
dstcolslice = std::slice(0, src_w, 1);
2) 如果是这样的话,那么整个If循环集不等价于以下内容吗?
offset_y = (offset_y >= 0) ? offset_y : 0;
offset_x = (offset_x >= 0) ? offset_x : 0;
for (Uint32 y = 0; y < src_h; y++) {
dstcolslice = std::slice(offset_x, (src_w + offset_x) * src_channels, 1);
dstval[y + offset_y][dstcolslice] = srcval[y][srccolslice];
}
相关文章:
- OpenMP阵列性能较差
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 位阵列上的快速AND运算
- 阵列必须使用大括号封闭的初始器进行初始化
- 没有从阵列<float>到阵列<int>的可行转换
- C++动态安全 2D 交错阵列
- 将平面阵列重塑为复杂的特征类型
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- 为什么我能够为阵列分配比计算机实际拥有的内存更多的内存
- 库特<<恩德尔;不适用于打印 2D 阵列
- 数组类 阵列的打印输出
- 从较小的阵列到较大的阵列的元素级转换
- 仅在大型阵列上出现合并排序分段错误
- 从阵列C++中删除重复项
- 阵列的大小在C++中是否灵活?
- 反转阵列中的正序列
- 具有可变尺寸的 C++ 2D 阵列
- 二维阵列的动态分配
- c++现有c阵列的焊盘侧