/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.util;

import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.WritableRaster;

/**
* @author Konstantin Bulenkov
*/
public class JBHiDPIScaledImage extends BufferedImage {
  private final Image myImage;
  private final int myWidth;
  private final int myHeight;

  /**
   * Creates a HiDPI-aware BufferedImage to draw on.
   *
   * @param width width in a user space coordinates
   * @param height height in a user space coordinates
   * @param type type of the created image
   */
  public JBHiDPIScaledImage(int width, int height, int type) {
    this(new BufferedImage(width * 2, height * 2, type), width, height, type);
  }

  /**
   * Creates a HiDPI-aware BufferedImage wrapping a delegate Image.
   *
   * @param image the delegate image of size in a device space coordinates to wrap
   * @param width width in a user space coordinates
   * @param height height in a user space coordinates
   * @param type type of the created image
   */
  public JBHiDPIScaledImage(@NotNull Image image, int width, int height, int type) {
    super(1, 1, type); // give the wrapper a minimal 1x1 size to save space
    myImage = image;
    myWidth = width;
    myHeight = height;
  }

  public Image getDelegate() {
    return myImage;
  }

  @Override
  public int getWidth() {
    return myWidth;
  }

  @Override
  public int getHeight() {
    return myHeight;
  }

  @Override
  public int getWidth(ImageObserver observer) {
    return myWidth;
  }

  @Override
  public int getHeight(ImageObserver observer) {
    return myHeight;
  }

  @Override
  public Graphics2D createGraphics() {
    assert myImage instanceof BufferedImage : "the delegate image is not BufferedImage";
    if (myImage instanceof BufferedImage) {
      return new HiDPIScaledGraphics(((BufferedImage)myImage).createGraphics());
    }
    return super.createGraphics();
  }

  @Override
  public WritableRaster getRaster() {
    assert myImage instanceof BufferedImage : "the delegate image is not BufferedImage";
    if (myImage instanceof BufferedImage) {
      return ((BufferedImage)myImage).getRaster();
    }
    return super.getRaster();
  }
}

@@ -626,51 +626,8 @@
 l;%0A%0A
-import org.jetbrains.annotations.NotNull;%0A%0A
 impo
@@ -718,46 +718,8 @@
 ver;
-%0Aimport java.awt.image.WritableRaster;
 %0A%0A/*
@@ -846,30 +846,24 @@
 ;%0A  private 
-final 
 int myWidth;
@@ -866,258 +866,91 @@
 dth;
-%0A
   
-private final int myHeight;%0A%0A  /**%0A   * Creates a HiDPI-aware BufferedImage to draw on.%0A   *%0A   * @param width width in a user space coordinates%0A   * @param height height in a user space coordinates%0A   * @param type type of the created image%0A   */
+// == myImage.width / scale%0A  private int myHeight; // == myImage.height / scale%0A
 %0A  p
@@ -1023,61 +1023,27 @@
 is(n
-ew BufferedImage(width * 2, height * 2, type),
+ull, 2 *
  width,
+ 2 *
  hei
@@ -1065,483 +1065,235 @@
 %0A%0A  
-/**%0A   * Creates a HiDPI-aware BufferedImage wrapping a delegate Image.%0A   *%0A   * @param image the delegate image of size in a device space coordinates to wrap%0A   * @param width width in a user space coordinates%0A   * @param height height in a user space coordinates%0A   * @param type type of the created image%0A   */%0A  public JBHiDPIScaledImage(@NotNull Image image, int width, int height, int type) %7B%0A    super(1, 1, type); // give the wrapper a minimal 1x1 size to save space
+public JBHiDPIScaledImage(Image image, int width, int height, int type) %7B%0A    // In case there's a delegate image, create a dummy wrapper image of 1x1 size%0A    super(image != null ? 1 : width, image != null ? 1 : height, type);
 %0A   
@@ -1458,37 +1458,74 @@
  %7B%0A    return my
-Width
+Image != null ? myWidth : super.getWidth()
 ;%0A  %7D%0A%0A  @Overri
@@ -1559,38 +1559,76 @@
  %7B%0A    return my
-Height
+Image != null ? myHeight : super.getHeight()
 ;%0A  %7D%0A%0A  @Overri
@@ -1691,21 +1691,66 @@
 eturn my
-Width
+Image != null ? myWidth : super.getWidth(observer)
 ;%0A  %7D%0A%0A 
@@ -1822,22 +1822,68 @@
 eturn my
-Height
+Image != null ? myHeight : super.getHeight(observer)
 ;%0A  %7D%0A%0A 
@@ -1940,532 +1940,133 @@
     
-assert myImage instanceof BufferedImage : %22the delegate image is not BufferedImage%22;%0A    if (myImage instanceof BufferedImage) %7B%0A      return new HiDPIScaledGraphics(((BufferedImage)myImage).createGraphics());%0A    %7D%0A    return super.createGraphics();%0A  %7D%0A%0A  @Override%0A  public WritableRaster getRaster() %7B%0A    assert myImage instanceof BufferedImage : %22the delegate image is not BufferedImage%22;%0A    if (myImage instanceof BufferedImage) %7B%0A      return ((BufferedImage)myImage).getRaster();%0A    %7D%0A    return super.getRaster()
+Graphics2D g = super.createGraphics();%0A    if (myImage == null) %7B%0A      return new HiDPIScaledGraphics(g);%0A    %7D%0A    return g
 ;%0A  

Rollback JBHiDPIScaledImage change.