/*
 * Copyright 2000-2014 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.rt.debugger;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by Egor on 04.10.2014.
 */
public class ImageSerializer {
  public static byte[] imageToBytes(Image image) throws IOException {
    BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    g.drawImage(image, 0, 0, null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bi, "png", baos);
    return baos.toByteArray();
  }

  public static byte[] iconToBytesPreview(Icon icon, int maxSize) throws IOException {
    if (icon.getIconHeight() <= maxSize && icon.getIconWidth() <= maxSize) {
      return imageToBytes(toImage(icon));
    }
    return null;
  }

  public static byte[] iconToBytesPreviewNormal(Icon icon) throws IOException {
    return iconToBytesPreview(icon, 16);
  }

  public static byte[] iconToBytesPreviewRetina(Icon icon) throws IOException {
    return iconToBytesPreview(icon, 32);
  }

  public static byte[] iconToBytes(Icon icon) throws IOException {
    return imageToBytes(toImage(icon));
  }

  // copied from IconUtils
  private static Image toImage(Icon icon) {
    if (icon instanceof ImageIcon) {
      return ((ImageIcon)icon).getImage();
    }
    else {
      final int w = icon.getIconWidth();
      final int h = icon.getIconHeight();
      final BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
      final Graphics2D g = image.createGraphics();
      icon.paintIcon(null, g, 0, 0);
      g.dispose();
      return image;
    }
  }

}

@@ -17,17 +17,17 @@
 2000-201
-4
+5
  JetBrai
@@ -815,38 +815,20 @@
 %0A * 
-Created by Egor on 04.10.2014.
+@author egor
 %0A */
@@ -925,24 +925,65 @@
 Exception %7B%0A
+    //noinspection UndesirableClassUsage%0A
     Buffered
@@ -1260,16 +1260,33 @@
  baos);%0A
+    g.dispose();%0A
     retu
@@ -1543,24 +1543,53 @@
  null;%0A  %7D%0A%0A
+  /** @noinspection unused*/%0A
   public sta
@@ -1694,32 +1694,61 @@
 icon, 16);%0A  %7D%0A%0A
+  /** @noinspection unused*/%0A
   public static 
@@ -1849,32 +1849,61 @@
 icon, 32);%0A  %7D%0A%0A
+  /** @noinspection unused*/%0A
   public static 

call graphics dispose after use