简体   繁体   English

如何重新排序原始图像颜色数据以从四个图像中实现特定的 2 x 2 格式? (C++)

[英]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.我有四个图像的原始颜色数据,我们称它们为 1、2、3 和 4。我将数据存储在分配了 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.我想创建 2 x 2 的原始图像数据以编码为单个图像。

1 2
3 4

For my example each image is 400 by 225 with RGBA (360000 bytes).在我的示例中,每个图像都是 400 x 225 的 RGBA(360000 字节)。 Iim doing a for loop with memcpy where我用 memcpy 做一个 for 循环 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).对于每个图像,其中添加了起始 position 的偏移量(上面的示例当然只适用于左上角)。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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