简体   繁体   中英

How to reorder raw image color data to achieve a specific 2 by 2 format from four images? (C++)

I have the raw color data for four images, let's call them 1, 2, 3, and 4. I am storing the data in an unsigned char * with allocated memory. Individually I can manipulate or encode the images but when trying to concatenate or order them into a single image it works but takes more time than I would like.

I would like to create a 2 by 2 of the raw image data to encode as a single image.

1 2
3 4

For my example each image is 400 by 225 with RGBA (360000 bytes). Iim doing a for loop with memcpy where

  for (int j = 0; j < 225; j++)
  {
   std::memcpy(dest + (j * (400 + 400) * 4), src + (j * 400 * 4), 400 * 4); //
  }

for each image with an offset for the starting position added in (the example above would only work for the top left of course).

This works but I'm wondering if this is a solved problem with a better solution, either in an algorithm described somewhere or a small library.

#include <iostream>

const int width  = 6;
const int height = 4;

constexpr int n  = width * height;

int main()
{
    unsigned char a[n], b[n], c[n], d[n];
    unsigned char dst[n * 4];

    int i = 0, j = 0;

    /* init data */
    for (; i < n; i++) {
        a[i] = 'a';
        b[i] = 'b';
        c[i] = 'c';
        d[i] = 'd';
    }

    /* re-order */
    i = 0;

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++, i++, j++) {
            dst[i                ] = a[j];
            dst[i         + width] = b[j];
            dst[i + n * 2        ] = c[j];
            dst[i + n * 2 + width] = d[j];
        }

        i += width;
    }

    /* print result */
    i = 0;

    for (int y = 0; y < height * 2; y++) {
        for (int x = 0; x < width * 2; x++, i++)
            std::cout << dst[i];

        std::cout << '\n';
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM