Ilja Kosynkin
Ilja Kosynkin

Reputation: 107

Android, Open GL ES, crop and scale texture

I have some troubles with video processing. I'm using Surface with Texture to process video with MediaCodec and MediaMuxer (decode, crop, encode with lower quality).
And on middle-step, which is cropping, I've got problems. Basically, what I want to do is to take lesser dimension of video, then, based on this side, define offsets for bigger side and crop it out. Then result should be properly scaled into 640x640 square. I searched for pretty big amount of time, but all information that I have found points that Open GL should scale image itself. Yes, I should admit that it's doing some scale, but result is looking very ugly it seems to be "compressed" verticaly quite hardly.
So, now the question itself. Can somebody provide to me some explanation or maybe even snipet of code that perfrom desired functions in Open GL ES?
I already tried to adjust Viewport thought.

GLES20.glViewport(0, 0, 640, 640)  

UPDATE
Idea that Isogen74 have proposed more less worked for me. It's still stretched quite significantly, but it's better then nothing.
Here you can find updated code: OpenGL setup

Upvotes: 1

Views: 2193

Answers (1)

solidpixel
solidpixel

Reputation: 12159

Cropping - change your texture coordinates that you loading from when loading the texture; e.g. if you you want to crop the top and bottom 10% off the image load from (0.0, 0.1) to (1.0, 0.9).

Scaling - the answer depends how big your downscale to 640*640 is. OpenGL filtering isn't designed to handle large downscaling ratios directly - it's not an image processing library - but assuming relatively small scaling ratios just ensure you have turned on GL_LINEAR texture filtering rather than GL_NEAREST.

If you have a large downscale you may need to mipmap the texture first and GL_LINEAR_MIPMAP_NEAREST or GL_LINEAR_MIPMAP_LINEAR as your minification filter, but just be aware that this isn't going to give you something as good as, for example a proper scaling algorithm you might get in image processing software (e.g. bicubic, or something like that).

Upvotes: 1

Related Questions