Sorry but I don't really have too much time to get very deeply into this right now...
I think Maa's suggestions will probably result in a more technically correct /useable transformation if you can implement it compared to the method I suggested (which may stil work anyway).
As far as your posted example goes - It appears there are two issues: jit.gradient works from left to right (rather than top to bottom as you require) so the resulting greyscale matrix needs to be rotated somehow, and 2: swapping inputs to the jit.op doing the scaling enables adjustments to the gradient without interrupting the process (also maybe try using @ op *)
HTH