Another zoom to cursor thread

Hello,

I have a 2D application which is an image viewer.
I have the ability to pan around the image in the window and I have a basic zoom.
Now I'd like to improve the zoom so it zoom to the point under the mouse.
So far I've read everything possible on google and i've come close to something working,
but still can't find something perfect, here is what I got:

it works well when the texture is filling all the window : i.e when the user zoom in a lot.
But when the image is small and does not fit yet in the viewer entirely ( there's black around), well it is messy and does not work...

btw, here is the code when the user zoom-in, in the mouse wheel event handler: