SpeedBirdNine
SpeedBirdNine

Reputation: 4676

How to read screen to see which pixels are white

I have a view on which the user can draw some lines, which have been developed using this.

Now the lines are drawn between points using the code:

- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
    static GLfloat*     vertexBuffer = NULL;
    static NSUInteger   vertexMax = 64;
    NSUInteger          vertexCount = 0,
                        count,
                        i;

    //Allocate vertex array buffer
    if(vertexBuffer == NULL)
        vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));

    // Add points to the buffer so there are drawing points every X pixels
    count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1);
    for(i = 0; i < count; ++i) {
        if(vertexCount == vertexMax) {
            vertexMax = 2 * vertexMax;
            vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
        }

        vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
        vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
        vertexCount += 1;
    }

    //Render the vertex array
    glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
    glDrawArrays(GL_POINTS, 0, vertexCount);

    // Display the buffer
    [self swapBuffers];
}

The objective is to read the drawing area of the screen which is initiated by the following code:

PictureView * scratchPaperView = [[RecordedPaintingView alloc] initWithFrame:CGRectMake(0, 45, 320, 415)];
    [self.view addSubview:scratchPaperView];

I want to find out the pixels of the lines, i.e. all the pixels that are white in the drawing area. Please tell me how to proceed from here?

Upvotes: 0

Views: 111

Answers (1)

David Hoerl
David Hoerl

Reputation: 41642

Assuming that you can get a UIImage.CGImage or a CGImageRef out of a PictureView, then you render this image into a CGBitMapContext. The image is going to tell you the number of components and if it has alpha and where alpa is. Most likely you are going to get 4 byte pixels (32 bits/pixel). You then walk each row looking at each pixel. Assuming a black background (which would be 255,0,0,0 or 0,0,0,255), you will see non-black pixels when you get close to or hit a line. A pure with pixel is going to be 255,255,255,255.

I'm pretty sure you can find examples of how to render an image into a context, and also how to examine pixels by googling around. Frankly what always gets me is the confusing pixel layout attributes - I usually end up printing a few test cases out to make sure I got it right.

Upvotes: 1

Related Questions