Q: How to create bulging effect is Java? I am trying to create a Java function to make a bulging effect on an image by shifting the pixel to the relative centre of the image. I first take the (x,y) coordinate of the pixel, find the relative shift, x = x-(x/2) and convert it to polar form [rcos(a), rsin(a)]. r is found by: r = Math.sqrt(xx + yy). Angle a is found using Math.atan2(y/x). New radius (r') is found using r' = 2r^1.5 . However, the new x,y values from [rcos(a), rsin(a)] exceed the dimensions of the image, and errors occur.
Am I making a fundamental mistake?
public void bulge()
  {
    double xval, yval = 0;
    //loop through the columns
    for(int x = 0; x < this.getWidth(); x++)
    {
      //loop through the rows
      for(int y = 0; y < this.getHeight(); y++)
      {
        int redValue, greenValue, blueValue = 0;
        double newRadius = 0;

        Pixel pixel = this.getPixel(x,y);
        redValue = pixel.getRed();
        greenValue = pixel.getGreen();
        blueValue = pixel.getBlue();

        xval = x - (x/2);
        yval = y - (y/2);

        double radius =  Math.sqrt(xval*xval + yval*yval);
        double angle = Math.atan2(yval, xval);
        newRadius = 2*(Math.pow(radius,1.5));
        xval = (int)(newRadius*Math.sin(angle));
        yval = (int)(newRadius*Math.cos(angle));

        Pixel pixelNewPos = this.getPixel((int)xval, (int)yval);
        pixelNewPos.setColor(new Color(redValue, greenValue, blueValue));
      }
    }
  }


A: 
Am I making a fundamental mistake?

At a conceptual level, yes.  Your algorithm is taking a rectangular image and moving the location of the pixels to give a larger, non-rectagular image.  Obviously that won't fit into your original rectangle.
So you either need to clip (i.e. discard) the pixels that fall outside of the rectangle, or you need to use a larger rectangle so that all of the mapped pixels fall inside it.  
In the latter case, there will be gaps around the edges ...if your transformation is doing what you claim it does.  A non-linear transformation of a rectangle is not going to have straight sides.

A: It's a lot easier to successfully apply a transform from source image A to destination image B by doing the reverse transform from pixels in image B to pixels in image A.
By this I mean for each pixel in destination image B, determine the pixel or pixels in source image A that contribute to the color.  That way you don't end up with a whole bunch of pixels in the target image that haven't been touched.
As an example using a linear scaling operation by 2, a simple implementation might look like this:
for (int x = 0; x < sourceWidth; ++x) {
  for (int y = 0; y < sourceHeight; ++y) {
    Pixel sourcePixel = sourceImage.getPixel(x, y);
    int destPixelX = x * 2;
    int destPixelY = y * 2;
    destImage.setPixel(destPixelX, destPixelY, sourcePixel);
  }
}

It should be clear from this code that pixels with either odd numbers X or Y values will not be set in the destination image.
A better way would be something like this:
for (int x = 0; x < destWidth; ++x) {
  for (int y = 0; y < destHeight; ++y) {
    int sourcePixelX = x / 2;
    int sourcePixelY = y / 2;
    Pixel sourcePixel = sourceImage.getPixel(sourcePixelX, sourcePixelY);
    destImage.setPixel(x, y, sourcePixel);
  }
}

Although this is not a good image upscaling algorithm in general, it does show how to make sure that all the pixels in your target image are set.
