public static @NotNull BufferedImage toBufferedImage(@NotNull Image image, boolean inUserSize, boolean ensureOneComponent) {
    if (image instanceof JBHiDPIScaledImage jbImage) {
      Image delegate = jbImage.getDelegate();
      if (delegate != null) image = delegate;
      if (inUserSize) {
        image = scaleImage(image, 1 / jbImage.getScale());
      }
    }
    if (image instanceof BufferedImage && (!ensureOneComponent || ((BufferedImage)image).getColorModel().getNumComponents() == 1)) {
      return (BufferedImage)image;
    }

    final int width = image.getWidth(null);
    final int height = image.getHeight(null);
    if (width <= 0 || height <= 0) {
      // avoiding NPE
      return new BufferedImage(Math.max(width, 1), Math.max(height, 1), BufferedImage.TYPE_INT_ARGB) {
        @Override
        public int getWidth() {
          return Math.max(width, 0);
        }

        @Override
        public int getHeight() {
          return Math.max(height, 0);
        }

        @Override
        public int getWidth(ImageObserver observer) {
          return Math.max(width, 0);
        }

        @Override
        public int getHeight(ImageObserver observer) {
          return Math.max(height, 0);
        }
      };
    }

    @SuppressWarnings("UndesirableClassUsage")
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bufferedImage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bufferedImage;
  }
public static @NotNull BufferedImage toBufferedImage(@NotNull Image image, boolean inUserSize, boolean ensureOneComponent)