/*
 * Copyright 2000-2009 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 org.intellij.images.util;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.SVGLoader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

/**
 * @author spleaner
 */
public class ImageInfoReader {
  private static final Logger LOG = Logger.getInstance("#org.intellij.images.util.ImageInfoReader");

  private ImageInfoReader() {
  }

  @Nullable
  public static Info getInfo(@NotNull String file) {
    try (FileInputStream fis = new FileInputStream(file)) {
      return read(fis);
    }
    catch (IOException e) {
      LOG.warn(e);
    }
    return null;
  }

  @Nullable
  public static Info getInfo(@NotNull byte[] data) {
    return read(new ByteArrayInputStream(data));
  }

  @Nullable
  private static Info read(@NotNull InputStream input) {
    try {
      Image load = SVGLoader.load(input, 1.0f);
      int bpp = -1;
      if (load instanceof BufferedImage) {
        bpp = ImageTypeSpecifier.createFromRenderedImage((BufferedImage)load).getColorModel().getPixelSize();
      }
      return new Info(load.getWidth(null), load.getHeight(null), bpp);
    } catch (IOException ignored) {}
    try (ImageInputStream iis = ImageIO.createImageInputStream(input)) {
      Iterator<ImageReader> it = ImageIO.getImageReaders(iis);
      ImageReader reader = it.hasNext() ? it.next() : null;
      if (reader != null) {
        reader.setInput(iis, true);
        int w = reader.getWidth(0);
        int h = reader.getHeight(0);
        Iterator<ImageTypeSpecifier> it2 = reader.getImageTypes(0);
        int bpp = it2 != null && it2.hasNext() ? it2.next().getColorModel().getPixelSize() : -1;
        return new Info(w, h, bpp);
      }
    }
    catch (Throwable e) {
      LOG.warn(e);
    }
    return null;
  }

  public static class Info {
    public int width;
    public int height;
    public int bpp;

    public Info(int width, int height, int bpp) {
      this.width = width;
      this.height = height;
      this.bpp = bpp;
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) return true;
      if (!(o instanceof Info)) return false;

      Info info = (Info)o;

      if (width != info.width) return false;
      if (height != info.height) return false;
      if (bpp != info.bpp) return false;

      return true;
    }

    @Override
    public int hashCode() {
      int result = width;
      result = 31 * result + height;
      result = 31 * result + bpp;
      return result;
    }
  }

}

@@ -680,44 +680,8 @@
 er;%0A
-import com.intellij.util.SVGLoader;%0A
 impo
@@ -929,175 +929,52 @@
 ava.
-awt.*;%0Aimport java.awt.image.BufferedImage;%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
+io.ByteArrayInputStream;%0Aimport java.io.File
 ;%0Aim
@@ -1268,162 +1268,35 @@
     
-try (FileInputStream fis = new FileInputStream(file)) %7B%0A      return read(fis);%0A    %7D%0A    catch (IOException e) %7B%0A      LOG.warn(e);%0A    %7D%0A    return null
+return read(new File(file))
 ;%0A  
@@ -1469,375 +1469,23 @@
 ull 
-InputStream input) %7B%0A    try %7B%0A      Image load = SVGLoader.load(input, 1.0f);%0A      int bpp = -1;%0A      if (load instanceof BufferedImage) %7B%0A        bpp = ImageTypeSpecifier.createFromRenderedImage((BufferedImage)load).getColorModel().getPixelSize();%0A      %7D%0A      return new Info(load.getWidth(null), load.getHeight(null), bpp);%0A    %7D catch (IOException ignored
+Object input
 ) %7B
-%7D
 %0A   

rollback indexing of svg