Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/65838477/how-to-reorder-raw-image-color-data-to-achieve-a-specific-2-by-2-format-from-fou

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#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 << '
';
    }

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...