我想要做的是,在 OpenGL 着色器中访问纹理的像素数据。之后,比较它们的红色分量,以便我可以得到具有最大红色分量的像素的坐标。我可以用 Objective C 和 CPU 处理能力来做到这一点。代码如下所示。
- (void)processNewPixelBuffer:(CVPixelBufferRef)pixelBuffer
{
short maxR = 0;
NSInteger x = -1, y = -1;
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
height = CVPixelBufferGetHeight(pixelBuffer);
width = CVPixelBufferGetWidth(pixelBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
uint8_t *src_buff = CVPixelBufferGetBaseAddress(pixelBuffer);
short** rawData = malloc(sizeof(short*) * height);
for (int i = 0; i < height; i++){
rawData[i] = malloc((sizeof(short) * width));
for (int j = 0; j < width; j++)
rawData[i][j] = (short)src_buff[(i + width * j) * 4];
}
for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++)
if (rawData[i][j] >= maxR) {
maxR = rawData[i][j];
x = i;
y = j;
}
free(rawData);
}
所以,我的问题是,我如何使用 GPU 来完成这个过程?我可以在 OpenGL 着色器中将 pixelBuffer 作为纹理。
顶点着色器
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}
片段着色器
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture; //The input sample, in RGBA format
void main()
{
// Code here
}
如何修改着色器以便找出具有最大红色分量的像素?然后,我想把那个像素变成红色,把其他像素变成白色。那可能吗?