What I want to do is, access the pixel data of a texture in the OpenGL Shader. After that, compair their Red-component so that I can get the coordinate of the pixel which has the maximum Red-component. I can do it with objective C, with CPU processing power. The code is shown below.
- (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);
}
So, my question is, how to I use GPU to do this process? I can make the pixelBuffer as a texture in the OpenGL Shader.
Vertex Shader
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}
Fragment Shader
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture; //The input sample, in RGBA format
void main()
{
// Code here
}
How do I modify the shader so that I can find out the pixel that has the maximum Red-component? Then, I want to turn that pixel to red color and other pixel to white. Is that possible?