代码之家  ›  专栏  ›  技术社区  ›  Perraco

如何将像素缓冲区复制到更大的缓冲区

  •  0
  • Perraco  · 技术社区  · 7 年前

    我试图复制一组像素到一个更大的像素缓冲区。很明显,我计算的坐标错了,可能是大踏步,但我找不到我遗漏的东西。我得到了一个完全混乱的结果。

    所以本质上,我要达到的是 RGBA公司 像素阵列变大 数组(alpha被丢弃),保持正确的步幅。有关预期结果的视觉表示,请参见下图。

    void draw(const uint8_t* tileRGBA, uint8_t* canvasRGB, const int canvasStride,
              const int tileWidth, const int tileHeight)
    {
        for (int y = 0; y < tileHeight; y++)
        {
            for (int x = 0; x < tileWidth; x++)
            {
                long tileIndex = (4 * x) + (y * tileWidth);
                long canvasIndex = (3 * x) + (y * canvasStride);
    
                canvasRGB[canvasIndex] = tileRGBA[tileIndex];
                canvasRGB[canvasIndex + 1] = tileRGBA[tileIndex + 1];
                canvasRGB[canvasIndex + 2] = tileRGBA[tileIndex + 2];
            }
        }
    }
    
    uint8_t* test(uint32_t* tileRGBA, const int tileWidth, const int tileHeight)
    {
        int canvasStride = tileWidth * 5; // 5 is just an arbitrary value for this example, in this case a canvas 5 times the width of the tile
    
        uint8_t* canvasRGB = new uint8_t[canvasStride * tileHeight * 3];
    
        draw((uint8_t*)tileRGBA, canvasRGB, canvasStride, tileWidth, tileHeight);
    
        return canvasRGB;
    }
    

    enter image description here

    enter image description here

    这:

    long tileIndex = (4 * x) + (y * tileWidth)
    long canvasIndex = (3 * x) + (y * canvasStride);
    

    一定是这样的:

    long tileIndex = 4 * (x + y * tileWidth);
    long canvasIndex = 3 * (x + y * canvasStride);
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   001    7 年前

    问题在于你对索引的计算。

    要在1D数组中获取二维索引(行、列),请执行以下操作:

    index = ((number_of_columns * row) + col) * sizeof(data)
    

    number_of_columns 是数据中预期的“列”数-在本例中是图像的宽度。以及 sizeof(data) 是数组中一个项的字节大小-在本例中,RGBA为4字节,RGB为3字节。所以,正如你所确定的,应该是:

    long tileIndex = 4 * (x + y * tileWidth);
    long canvasIndex = 3 * (x + y * canvasStride);
    

    sizeof 如果可以将数据表示为单个项,则为乘法。例如,在您的示例中,创建2个结构:

    struct RGB {
        uint8_t r,g,b;
    };
    struct RGBA {
        uint8_t r,g,b,a;
    };
    

    void draw(const RGBA* tileRGBA, RGB* canvasRGB, const int canvasStride, const int tileWidth, const int tileHeight)
    

    然后,计算简化为:

    long tileIndex = x + y * tileWidth;
    long canvasIndex = x + y * canvasStride;
    
    推荐文章