I've an WPF Image control, which use scale shader for zooming around center of the image (ScaleCenter is always 0.5,0.5). But for zooming around specific point on the image (cursor) I guess i should apply some offests to keep the same image pixel under cursor.
I've this code which is works perfect untill Scaleshader < 1.
public void ScalePoint(double zoomFactor, Point point, Image ImageViewElement, bool pushOperation = false)
{
double w = ImageViewElement.ActualWidth;
double h = ImageViewElement.ActualHeight;
double cx = w * 0.5;
double cy = h * 0.5;
double dx = point.X - cx;
double dy = point.Y - cy;
double contentX = (OffsetX + dx) / ScaleShader;
double contentY = (OffsetY + dy) / ScaleShader;
OffsetX = contentX * ScaleShader * zoomFactor - dx;
OffsetY = contentY * ScaleShader * zoomFactor - dy;
var m = new Matrix();
m.Translate(-OffsetX, -OffsetY);
RenderTransform = new TransformGroup { Children = { new MatrixTransform(m) } };
ScaleShader *= zoomFactor;
}
When the ScaleShader becomes > 1 - these offsets OffsetX and OffsetY - I guess calculating in a wrong way.
Could you give me some hint on how this parameters should be calculated for any scaleShader values? (I cannot use only matrix transform/scale, because the image sizes are big, so we should use Shader but as I said, scale_center is always 0.5,05 and it cannot be changed because it is used in other parts of the software.
shader code
sampler2D inputSampler : register(S0);
sampler2D inputNNSampler : register(S1);
float scale : register(C0);
float2 scale_center : register(C1); // always (0.5, 0.5)
float scaling_mode : register(C2);
float4 main(float2 uv : TEXCOORD) : COLOR
{
float2 ray = uv - scale_center;
uv = scale_center + ray / scale;
if (uv.x < 0 || uv.y < 0 || uv.x > 1 || uv.y > 1)
return 0;
return scaling_mode > 0
? tex2D(inputNNSampler, uv)
: tex2D(inputSampler, uv);
}