0

我有一个 TGA 文件和一个库,它已经拥有阅读 TGA 和使用它们所需的一切。

该类有一个名为pixels()的方法,它返回一个指针,该指针指向像素存储为RGBRGBRGB的内存区域...

我的问题是,如何获取像素值?

因为如果我做这样的事情:

img.load("foo.tga");
printf ("%i", img.pixels());

它给了我大概的地址。

我在这个网站上找到了这段代码:

struct Pixel2d
{
    static const int SIZE = 50;
    unsigned char& operator()( int nCol,  int nRow, int RGB)
    {
        return pixels[ ( nCol* SIZE + nRow) * 3 + RGB];
    }

    unsigned char pixels[SIZE * SIZE * 3 ];
};

int main()
{

    Pixel2d p2darray;
    glReadPixels(50,50, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &p.pixels);

    for( int i = 0; i < Pixel2d::SIZE ; ++i )
    {
        for( int j = 0; j < Pixel2d::SIZE ; ++j )
        {
            unsigned char rpixel = p2darray(i , j , 0);
            unsigned char gpixel = p2darray(i , j , 1);
            unsigned char bpixel = p2darray(i , j , 2);
        }
    }
}

我认为它对我很有用,但是我怎样才能告诉程序从我的 img 中读取呢?

4

1 回答 1

1

Tga 支持不同的像素深度。而且我们不知道您使用的是什么库。但一般来说,pixels() 应该返回一个指向包含像素的缓冲区的指针。假设为了论证,它将像素解压缩为每通道 8 位的子像素,然后每个像素由 3 个字节表示。

因此,要访问缓冲区中给定偏移量的像素:

const u8* pixelBuffer = img.pixels():

u8 red   = pixelBuffer[(offset*3)+0];
u8 green = pixelBuffer[(offset*3)+1];
u8 blue  = pixelBuffer[(offset*3)+2];

如果您知道图像缓冲区的宽度,那么您可以通过其 x 和 y 坐标获得一个像素:

u8 red = pixelBuffer[((x+(y*width))*3)+0];
于 2012-10-23T15:26:01.493 回答