public Texture getTexture() {
	ImageComponent2D[] scaledImageComponents = null;
	BufferedImage[] scaledBufferedImages = null;
        if (tex == null) {

          int width;
          int height;

          if (forcePowerOfTwo) {
              width = getClosestPowerOf2(bufferedImage.getWidth());
              height = getClosestPowerOf2(bufferedImage.getHeight());
	  } else {
              width = bufferedImage.getWidth();
              height = bufferedImage.getHeight();
	  }

	  if ((flags & GENERATE_MIPMAP) != 0) {

	    BufferedImage origImage = bufferedImage;
	    int newW = width;
	    int newH = height;
	    int level = Math.max(computeLog(width), computeLog(height)) + 1;
	    scaledImageComponents = new ImageComponent2D[level];
	    scaledBufferedImages = new BufferedImage[level];
            tex = new Texture2D(tex.MULTI_LEVEL_MIPMAP, textureFormat,
                width, height);

            for (int i = 0; i < level; i++) {
                scaledBufferedImages[i] = getScaledImage(origImage, newW, newH);
                scaledImageComponents[i] =  new ImageComponent2D(
			imageComponentFormat, scaledBufferedImages[i],
			byRef, yUp);

                tex.setImage(i, scaledImageComponents[i]);
		if (forcePowerOfTwo) {
		    if (newW > 1) newW >>= 1;
		    if (newH > 1) newH >>= 1;
		} else {
		    if (newW > 1) {
			newW = (int) Math.floor(newW / 2.0);
		    }
		    if (newH > 1) {
			newH = (int) Math.floor(newH / 2.0);
		    }
		}
	        origImage = scaledBufferedImages[i];
            }

          } else {
	    scaledImageComponents = new ImageComponent2D[1];
	    scaledBufferedImages = new BufferedImage[1];

            // Create texture from image
            scaledBufferedImages[0] = getScaledImage(bufferedImage,
			width, height);
            scaledImageComponents[0] = new ImageComponent2D(
			imageComponentFormat, scaledBufferedImages[0],
			byRef, yUp);

            tex = new Texture2D(tex.BASE_LEVEL, textureFormat, width, height);

            tex.setImage(0, scaledImageComponents[0]);
          }
          tex.setMinFilter(tex.BASE_LEVEL_LINEAR);
          tex.setMagFilter(tex.BASE_LEVEL_LINEAR);
        }

	return tex;
    }