簡體   English   中英

如何在 sfml 中使用圖像中的像素陣列

[英]How to use the pixel array from an image in sfml

我是 sfml 的新手,想分別處理圖像的每個像素。

我看到的最好方法是遍歷此圖像中所有像素的數組。 Sfml 為此提供了方便的函數sf::Image::getPixelsPtr() 不幸的是,我無法理解返回的數組。

我不明白這方面的文檔,我究竟如何才能訪問此數組中某個像素的顏色值?

const Uint8* sf::Image::getPixelsPtr () const

據我所知,圖像是 8 位 RGBA,他們在圖像的開頭給你一個 Uint8 指針,這將是第一個像素的“R”。

// code edited. was saying 'sd' i changed to 'sf'
const UInt8* pByteBuffer = sf::Image::getPixelsPtr();

size_t numPixels = getSize().x + getSize().y;
for (int i = 0 ; i < numPixels; ++i)
    {
    UInt8 red   = pByteBuffer[4 * i];
    UInt8 green = pByteBuffer[4 * i + 1];
    UInt8 blue  = pByteBuffer[4 * i + 2];
    UInt8 alpha = pByteBuffer[4 * i + 3];
    }

如果你想迭代整個像素而不是像素內的單一顏色,你可以 reinterpret_cast 他們給你的指針:

const UInt8* pByteBuffer = sf::Image::getPixelsPtr();
const UInt32* pPixelBuffer = reinterpret_cast<const UInt32*>(pByteBuffer);

size_t numPixels = getSize().x * getSize().y;
for (int i = 0; i < numPixels; ++i)
    {
    uint32_t pixel = pPixelBuffer[i];
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM