I'm getting there, but still needs work.
Here is what i have so far:
/* get the_image ... */
QImage originalImage
= the_image;
QImage warpedImage
= originalImage.
copy();
const QRgb* oldPixels = (QRgb*)originalImage.bits();
QRgb* newPixels = (QRgb*)warpedImage.bits();
const int imgWidth = warpedImage.width();
const int imgHeight = warpedImage.height();
const int halfWidth = imgWidth / 2;
const int halfHeight = imgHeight / 2;
for (int y = 0; y < imgHeight; y++) {
for (int x = 0; x < imgWidth; x++) {
float cx = (float)(x - halfWidth);
float cy = (float)(y - halfHeight);
float radiusSquared = (cx * cx) + (cy * cy);
float f = 1 - (radiusSquared * 0.0005f);
int newX = (int)(f * cx) + halfWidth;
int newY = (int)(f * cy) + halfHeight;
if (newX >= 0 && newY >= 0 && newX < imgWidth && newY < imgHeight) {
*(newPixels + (y * imgWidth) + x) = *(oldPixels + (newY * imgWidth) + newX);
}
}
}
/* draw warpedImage ... */
/* get the_image ... */
QImage originalImage = the_image;
QImage warpedImage = originalImage.copy();
const QRgb* oldPixels = (QRgb*)originalImage.bits();
QRgb* newPixels = (QRgb*)warpedImage.bits();
const int imgWidth = warpedImage.width();
const int imgHeight = warpedImage.height();
const int halfWidth = imgWidth / 2;
const int halfHeight = imgHeight / 2;
for (int y = 0; y < imgHeight; y++) {
for (int x = 0; x < imgWidth; x++) {
float cx = (float)(x - halfWidth);
float cy = (float)(y - halfHeight);
float radiusSquared = (cx * cx) + (cy * cy);
float f = 1 - (radiusSquared * 0.0005f);
int newX = (int)(f * cx) + halfWidth;
int newY = (int)(f * cy) + halfHeight;
if (newX >= 0 && newY >= 0 && newX < imgWidth && newY < imgHeight) {
*(newPixels + (y * imgWidth) + x) = *(oldPixels + (newY * imgWidth) + newX);
}
}
}
/* draw warpedImage ... */
To copy to clipboard, switch view to plain text mode
The main thing is calculating the offsets (deflection) of each pixel. That is the line:
f = 1 - (radiusSquared * scaleFactor)
With that minus in there, the image curves out toward the edges (like this). I want it to distort the other way, like a magnifying glass. But when i change the minus to a plus, the result is not quite right. I think it is curving the image correctly, but only the middle of the image, the outside is unaffected.
I will continue to play around with the algorithm until i get something that looks good.
Just one more question, is that algorithm above a linear function or sinusoidal? (i think it's linear, right?).
Bookmarks