Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ Pad sides of existing C array
    primarykey
    data
    text
    <p>I have a C array, i.e. <code>&lt;type&gt;* myarray</code>, that is logically a <code>&lt;type&gt;* myarray[#][#]</code>. It is the data from an RGBA image. I want to enlarge the "image" by shifting the contents by <code>offsetx, offsety</code> and filling the extra space with zeros.</p> <pre><code>01 02 03 04 05 06 07 08 09 10 11 12 </code></pre> <p>with offset <code>1, -1</code> yields</p> <pre><code>00 01 02 03 04 00 05 06 07 08 00 09 10 11 12 00 00 00 00 00 </code></pre> <p>There is obviously an efficient way to do this, but I'm stumped. </p> <p>Edit01: I'm trying to do something along the lines of:</p> <pre><code>/** * @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 -&gt; 4, HSV32f32f32f -&gt; 3. * @param offset_y Vert. offset * @param offset_x Horiz. offset */ template &lt;typename arg_type&gt; 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&lt;arg_type&gt; srcval = std::valarray&lt;arg_type&gt;(src_img, sizeof(arg_type) * src_pixels); Uint32 dst_pixels = (src_h + abs(offset_y)) * (src_w + abs(offset_x)) * src_channels; std::valarray&lt;arg_type&gt; dstval = std::valarray&lt;arg_type&gt;((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 &lt; 0 ) { // Top Row -&gt; Top Row for (Uint32 y = 0; y &lt; src_h; y++) { // Left Col -&gt; Left Col if (offset_x &lt; 0) { dstcolslice = std::slice(0, src_w * src_channels, 1); } else { // Right Col -&gt; Right Col dstcolslice = std::slice(offset_x, (src_w + offset_x) * src_channels, 1); } dstval[y][dstcolslice] = srcval[y][srccolslice]; } } else { // Bot Row -&gt; Bot Row for (Uint32 y = 0; y &lt; src_h; y++) { // Left Col -&gt; Left Col if (offset_x &lt; 0) { dstcolslice = std::slice(0, src_w, 1); } else { // Right Col -&gt; Right Col dstcolslice = std::slice(offset_x, (src_w + offset_x) * src_channels, 1); } dstval[y + offset_y][dstcolslice] = srcval[y][srccolslice]; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload