c++现有c阵列的焊盘侧

c++ Pad sides of existing C array

本文关键字:焊盘 阵列 现有 c++      更新时间:2023-10-16

我有一个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];
}