GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

When using front camera, image is upside down in landscape

Open jim-rhoades opened this issue 14 years ago • 12 comments

Somehow Apple's GLCameraRipple sample project handles this, though I haven't quite figured out how yet. Maybe it's handled by the shader itself? (some I know nothing about, for now...)

I also looked at RosyWriter, which has a similar problem with the front camera.

Thanks Brad for creating this... after a couple of days of testing, I'm pretty sure it will replace Core Image in a project I'm working on.

jim-rhoades avatar Feb 25 '12 00:02 jim-rhoades

Each of the iOS device cameras has a different orientation. My iPhone 4, iPhone 4S, and iPad 2 rear cameras are all landscape left. I believe the front cameras may be landscape right. Within my sample applications, I apply a GPUImageRotationFilter with a setting of kGPUImageRotateRight on the output from the camera to reorient that, so the opposite rotation could be applied to the front-facing camera. However, there might be a better way to handle this, so I'm looking into it.

BradLarson avatar Feb 25 '12 16:02 BradLarson

I noticed the GPUImageRotationFilter already has modes for flipping images vertically or horizontally. This is what the standard Apple Camera app does when using the front camera, basically flipping the image so it's mirrored.

So, one fix for using the front camera is just to add second GPUImageRotationFilter that flips the image:

GPUImageRotationFilter *rotationFilter = [[GPUImageRotationFilter alloc] initWithRotation:kGPUImageRotateRight];
GPUImageRotationFilter *flipFilter = [[GPUImageRotationFilter alloc] initWithRotation:kGPUImageFlipVertical];

[videoCamera addTarget:rotationFilter];
[rotationFilter addTarget:flipFilter];
[flipFilter addTarget:filterView];

I imagine there's a way to do it all in one new "rotate right" + "flip vertical" option to the rotation filter, so I'm going to start playing around with that.

jim-rhoades avatar Mar 06 '12 16:03 jim-rhoades

I figured it out after a bit of guesswork.

I don't know how to use Git/GitHub yet... but here is a revised GPUImageRotationFilter.m if you want it, that adds a constant for "kGPUImageRotateRightFlipVertical" that can be used for orienting/mirroring the video for the front camera (tested only on an iPhone 4S):

#import "GPUImageRotationFilter.h"

NSString *const kGPUImageRotationFragmentShaderString =  SHADER_STRING
(
 varying highp vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;

 void main()
 {
     gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
 }
);

@implementation GPUImageRotationFilter

#pragma mark -
#pragma mark Initialization and teardown

- (id)initWithRotation:(GPUImageRotationMode)newRotationMode;
{
    if (!(self = [super initWithFragmentShaderFromString:kGPUImageRotationFragmentShaderString]))
    {
        return nil;
    }

    rotationMode = newRotationMode;

    return self;
}


#pragma mark -
#pragma mark GPUImageInput

- (void)setInputSize:(CGSize)newSize;
{
    if ( (rotationMode == kGPUImageRotateLeft) || (rotationMode == kGPUImageRotateRight) )
    {
        inputTextureSize.width = newSize.height;
        inputTextureSize.height = newSize.width;
    }
    else
    {
        inputTextureSize = newSize;
    }
}

- (void)newFrameReady;
{
    static const GLfloat rotationSquareVertices[] = {
        -1.0f, -1.0f,
        1.0f, -1.0f,
        -1.0f,  1.0f,
        1.0f,  1.0f,
    };

    static const GLfloat rotateLeftTextureCoordinates[] = {
        1.0f, 0.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        0.0f, 1.0f,
    };

    static const GLfloat rotateRightTextureCoordinates[] = {
        0.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 1.0f,
        1.0f, 0.0f,
    };

    static const GLfloat verticalFlipTextureCoordinates[] = {
        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,
    };

    static const GLfloat horizontalFlipTextureCoordinates[] = {
        1.0f, 0.0f,
        0.0f, 0.0f,
        1.0f, 1.0f,
        0.0f, 1.0f,
    };

    static const GLfloat rotateRightVerticalFlipTextureCoordinates[] = {
        0.0f, 0.0f,
        0.0f, 1.0f,
        1.0f, 0.0f,
        1.0f, 1.0f,
    };

    switch (rotationMode)
    {
        case kGPUImageRotateLeft: [self renderToTextureWithVertices:rotationSquareVertices textureCoordinates:rotateLeftTextureCoordinates]; break;
        case kGPUImageRotateRight: [self renderToTextureWithVertices:rotationSquareVertices textureCoordinates:rotateRightTextureCoordinates]; break;
        case kGPUImageFlipHorizonal: [self renderToTextureWithVertices:rotationSquareVertices textureCoordinates:verticalFlipTextureCoordinates]; break;
        case kGPUImageFlipVertical: [self renderToTextureWithVertices:rotationSquareVertices textureCoordinates:horizontalFlipTextureCoordinates]; break;
        case kGPUImageRotateRightFlipVertical: [self renderToTextureWithVertices:rotationSquareVertices textureCoordinates:rotateRightVerticalFlipTextureCoordinates]; break;
    }

}

@end

And obviously the enum needs to be changed in GPUImageRotationFilter.h as well:

typedef enum { kGPUImageRotateLeft, kGPUImageRotateRight, kGPUImageFlipVertical, kGPUImageFlipHorizonal, kGPUImageRotateRightFlipVertical} GPUImageRotationMode;

jim-rhoades avatar Mar 06 '12 19:03 jim-rhoades

I've added this rotation mode into the main repository, thanks for providing it. I'm still working on a more automatic way to handle this, though.

BradLarson avatar Mar 06 '12 19:03 BradLarson

kGPUImageRotateRightFlipVertical than kGPUImageRotateRight slow processing speed

brewswang avatar Apr 11 '12 06:04 brewswang

I think it's an urgent problem. Users need to take head pictures. It's not a good resolution using RotationFilter twice. Is there a good one?

fengally avatar Apr 24 '12 03:04 fengally

And my resolution is that pass the position into the rotationFilter and handle this in there rotation filter.

fengally avatar Apr 24 '12 03:04 fengally

I'm confused about what's wrong here. If you want to use the front-facing camera in portrait mode, simply use a rotation filter with the kGPUImageRotateRightFlipVertical rotation mode instead of kGPUImageRotateRight. Two rotation filters are not needed, and kGPUImageRotateRightFlipVertical is no slower than kGPUImageRotateRight.

I just tried, and this works perfectly fine for capturing from the front-facing camera.

BradLarson avatar Apr 24 '12 18:04 BradLarson

i use kGPUImageRotateRight and kGPUImageFlipVertical,and this works perfectly fine,speed fast than kGPUImageRotateRightFlipVertical.

brewswang avatar Apr 28 '12 10:04 brewswang

Using kGPUImageRotateRightFlipVertical definitely shouldn't be any slower than applying two separate rotation filters... so I'm not sure how you're seeing that.

jim-rhoades avatar Apr 28 '12 19:04 jim-rhoades

on iphone4s may be same, but did you run on iphone4?

brewswang avatar May 02 '12 06:05 brewswang

OK, so I just benchmarked this. Using the front camera with a rotation filter set to kGPUImageRotateRightFlipVertical takes 1.9 ms on average to upload, process using a simple filter, and display to the screen on an iPhone 4. Using the front camera with just a kGPUImageRotateRight rotational filter also takes 1.9 ms for this same operation. Using the front camera and two rotational filters, one set to kGPUImageRotateRight and the next one set to kGPUImageFlipVertical takes on average 2.1 ms to process.

Using two filters is clearly slower than using one, and the kGPUImageRotateRightFlipVertical is no slower than a kGPUImageRotateRight. The only difference between the rotations are the coordinates used, and the processing is otherwise identical. Using two filters is always going to be slower. I have no idea what you are doing to have the one filter seem slower to you in your application, but it clearly isn't.

BradLarson avatar May 04 '12 21:05 BradLarson