a GPU based cropper?
Hi all,
I'm looking for a more efficient means of cropping an image. the goal is to crop, but not resize....i.e. where the image is cropped is just black. I have a patch below which works fine for what I need, however this method adds about 40-50% to my CPU...cycles I would much rather push to the GPU. Are there any shaders that do something similar? Again, patch below which hopefully illustrates what I'm trying to do.
Much thanks!
David
David,
Here is a patch with a shader I use to crop...
Maybe it will be usefull for you.
regards,
r
PS : the 2 shaders belows need to be saved on your hard drive with the correct name (see below the patch)
shader file : need to be named m_crop.jxs
define crop param
define edge size
zoom factor
offset factor for zoom
interpolation on/off
frament shader file : need to be named m_crop.fp.glsl
// texcoords
varying vec2 texcoord0;
varying vec2 texdim0;
// samplers
uniform sampler2DRect tex0;
// resample dimensions
uniform vec4 crop;
uniform vec4 edge;
uniform vec2 zoom ;
uniform vec2 offset ;
uniform float interp;
// entry point
void main()
{
// ramene coord [0-1] sur [0-texdim]
vec4 cm = crop*vec4(texdim0.xyxy);
vec4 am = edge*vec4(texdim0.xyxy);
// calcul coord pixel fct zoom et offset
vec2 fragcoord ;
fragcoord = texcoord0/zoom - offset*texdim0 ;
// verifie que dans borne du crop
bool bnd = all(bvec4(texcoord0.x>cm.x,texcoord0.xcm.y,texcoord0.y
// recupere couleur pixel
vec4 fragColor = texture2DRect(tex0,mix(floor(fragcoord),fragcoord,interp));
float alpha = fragColor.a ;
// alpha blending hors des bornes
if (bnd) {
float ax = 1.;
float ay = 1. ;
// a = smoothstep (cm, am, texcoord0.xyxy) ;
if (texcoord0.x
ax = smoothstep (cm.x, cm.x+am.x, texcoord0.x) ;
}else if (texcoord0.x>cm.z - am.z){
ax = smoothstep (cm.z, cm.z-am.z, texcoord0.x) ;
}
if (texcoord0.y
ay = smoothstep (cm.y, cm.y+am.y, texcoord0.y) ;
}else if (texcoord0.y>cm.w - am.w){
ay = smoothstep (cm.w, cm.w-am.w, texcoord0.y) ;
}
fragColor.a = clamp(ax*ay, 0., alpha) ;
}
else {
fragColor.a = 0. ;
}
gl_FragColor = fragColor ;
}
This is perfect! Thanks so much for your help with this. I really do need to start learning how to use shaders!
Thanks again!
David
sorry for the question:
how exactly should i save the shader?
do i have to add an header?
i have exactly the same problem/question: how do i get this shader to work?
even if i do add a
thanks for help!
k
here's one using gl.pix:
thanks for the quick reply!
in the meantime i as well found td.resample.jxs which does the job nicely...
Hi, Rob Ramirez. Thanks for the jit.gl.pix example!
But, as I understand it, it's essentially a smart workaround, i.e, you're masking the unwanted area through an alpha channel instead or really cropping the texture. Imagine that following the cropping process there's a long chain of slab processing. The GPU would be processing a larger size texture than really needed, lowering the performance. Would it be possible to build a jit.gl.pix that really crops the image, i.e, outputs a texture with smaller dimensions?
Thanks!
A bit an old thread but, like Pedro, I'm wondering if it's possible to actually crop the texture?
thanks
if you want to change the actual texture dims, set @adapt 0 and @dim width height on the slab/pix/texture object.
very nice!
Nice patch Pedro. Thanks for the share.
Woh... This is very nice... Pedro. Thanks for sharing! :)