code: protected static Rectangle getRectangle (BufferedImage img, boolean stretch)
      {
          Rectangle rect = new Rectangle(0, 0, img.getWidth() - 2, img.getHeight() - 2);
  
          for (int xx = 1; xx < rect.width + 1; xx++) {
              if (ImageUtil.hitTest(img, xx, stretch ? 0 : img.getHeight() - 1)) {
                  rect.x = xx - 1;
                  rect.width -= rect.x;
                  break;
              }
          }
  
          for (int xx = img.getWidth() - 1; xx >= rect.x + 1; xx--) {
              if (ImageUtil.hitTest(img, xx, stretch ? 0 : img.getHeight() - 1)) {
                  rect.width = xx - rect.x;
                  break;
              }
          }
  
          for (int yy = 1; yy < rect.height + 1; yy++) {
              if (ImageUtil.hitTest(img, stretch ? 0 : img.getWidth() - 1, yy)) {
                  rect.y = yy - 1;
                  rect.height -= rect.y;
                  break;
              }
          }
  
          for (int yy = img.getHeight() - 1; yy >= rect.y + 1; yy--) {
              if (ImageUtil.hitTest(img, stretch ? 0 : img.getWidth() - 1, yy)) {
                  rect.height = yy - rect.y;
                  break;
              }
          }
  
          return rect;
      }
docs: Parses the image to find the bounds of the rectangle defined by pixels on the outside.