簡體   English   中英

如何重新排序原始圖像顏色數據以從四個圖像中實現特定的 2 x 2 格式? (C++)

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

我有四個圖像的原始顏色數據,我們稱它們為 1、2、3 和 4。我將數據存儲在分配了 memory 的無符號字符 * 中。 我可以單獨操作或編碼圖像,但是當嘗試將它們連接或排序為單個圖像時,它可以工作,但需要的時間比我想要的要多。

我想創建 2 x 2 的原始圖像數據以編碼為單個圖像。

1 2
3 4

在我的示例中,每個圖像都是 400 x 225 的 RGBA(360000 字節)。 我用 memcpy 做一個 for 循環 where

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

對於每個圖像,其中添加了起始 position 的偏移量(上面的示例當然只適用於左上角)。

這行得通,但我想知道這是否是一個有更好解決方案的已解決問題,無論是在某處描述的算法還是在小型庫中。

#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