Add support for loading 9-patch from a stream.

This will be used by the tests.
Also added the eclipse project files.

Change-Id: I56443773d90594475fc3f14464d48e56d5a1bbb5
diff --git a/tools/ninepatch/.classpath b/tools/ninepatch/.classpath
new file mode 100644
index 0000000..fb50116
--- /dev/null
+++ b/tools/ninepatch/.classpath
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/tools/ninepatch/.project b/tools/ninepatch/.project
new file mode 100644
index 0000000..192f000
--- /dev/null
+++ b/tools/ninepatch/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>ninepatch</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
diff --git a/tools/ninepatch/src/com/android/ninepatch/GraphicsUtilities.java b/tools/ninepatch/src/com/android/ninepatch/GraphicsUtilities.java
index 7a823ec..e8d71d6 100644
--- a/tools/ninepatch/src/com/android/ninepatch/GraphicsUtilities.java
+++ b/tools/ninepatch/src/com/android/ninepatch/GraphicsUtilities.java
@@ -25,6 +25,7 @@
 import java.awt.Transparency;
 import java.net.URL;
 import java.io.IOException;
+import java.io.InputStream;
 
 public class GraphicsUtilities {
     public static BufferedImage loadCompatibleImage(URL resource) throws IOException {
@@ -32,6 +33,11 @@
         return toCompatibleImage(image);
     }
 
+    public static BufferedImage loadCompatibleImage(InputStream stream) throws IOException {
+        BufferedImage image = ImageIO.read(stream);
+        return toCompatibleImage(image);
+    }
+
     public static BufferedImage createCompatibleImage(int width, int height) {
         return getGraphicsConfiguration().createCompatibleImage(width, height);
     }
diff --git a/tools/ninepatch/src/com/android/ninepatch/NinePatch.java b/tools/ninepatch/src/com/android/ninepatch/NinePatch.java
index 35a1824..7484fd8 100644
--- a/tools/ninepatch/src/com/android/ninepatch/NinePatch.java
+++ b/tools/ninepatch/src/com/android/ninepatch/NinePatch.java
@@ -21,6 +21,7 @@
 import java.awt.RenderingHints;
 import java.awt.image.BufferedImage;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -33,7 +34,7 @@
     public static final String EXTENSION_9PATCH = ".9.png";
 
     private BufferedImage mImage;
-    
+
     private int mMinWidth;
     private int mMinHeight;
 
@@ -50,7 +51,7 @@
 
     private Pair<Integer> mHorizontalPadding;
     private Pair<Integer> mVerticalPadding;
-    
+
     private float mHorizontalPatchesSum;
     private float mVerticalPatchesSum;
 
@@ -58,12 +59,10 @@
 
     private int mRemainderVertical;
 
-    private final URL mFileUrl;
-
     /**
      * Loads a 9 patch or regular bitmap.
      * @param fileUrl the URL of the file to load.
-     * @param convert if <code>true</code>, non 9-patch bitmpa will be converted into a 9 patch.
+     * @param convert if <code>true</code>, non 9-patch bitmap will be converted into a 9 patch.
      * If <code>false</code> and the bitmap is not a 9 patch, the method will return
      * <code>null</code>.
      * @return a {@link NinePatch} or <code>null</code>.
@@ -77,9 +76,46 @@
             // really this shouldn't be happening since we're not creating the URL manually.
             return null;
         }
-        
+
         boolean is9Patch = fileUrl.getPath().toLowerCase().endsWith(EXTENSION_9PATCH);
-        
+
+        return load(image, is9Patch, convert);
+    }
+
+    /**
+     * Loads a 9 patch or regular bitmap.
+     * @param stream the {@link InputStream} of the file to load.
+     * @param is9Patch whether the file represents a 9-patch
+     * @param convert if <code>true</code>, non 9-patch bitmap will be converted into a 9 patch.
+     * If <code>false</code> and the bitmap is not a 9 patch, the method will return
+     * <code>null</code>.
+     * @return a {@link NinePatch} or <code>null</code>.
+     * @throws IOException
+     */
+    public static NinePatch load(InputStream stream, boolean is9Patch, boolean convert)
+            throws IOException {
+        BufferedImage image = null;
+        try {
+            image  = GraphicsUtilities.loadCompatibleImage(stream);
+        } catch (MalformedURLException e) {
+            // really this shouldn't be happening since we're not creating the URL manually.
+            return null;
+        }
+
+        return load(image, is9Patch, convert);
+    }
+
+    /**
+     * Loads a 9 patch or regular bitmap.
+     * @param image the source {@link BufferedImage}.
+     * @param is9Patch whether the file represents a 9-patch
+     * @param convert if <code>true</code>, non 9-patch bitmap will be converted into a 9 patch.
+     * If <code>false</code> and the bitmap is not a 9 patch, the method will return
+     * <code>null</code>.
+     * @return a {@link NinePatch} or <code>null</code>.
+     * @throws IOException
+     */
+    public static NinePatch load(BufferedImage image, boolean is9Patch, boolean convert) {
         if (is9Patch == false) {
             if (convert) {
                 image = convertTo9Patch(image);
@@ -90,10 +126,9 @@
             ensure9Patch(image);
         }
 
-        
-        return new NinePatch(fileUrl, image);
+        return new NinePatch(image);
     }
-    
+
     public int getWidth() {
         return mImage.getWidth() - 2;
     }
@@ -101,9 +136,9 @@
     public int getHeight() {
         return mImage.getHeight() - 2;
     }
-    
+
     /**
-     * 
+     *
      * @param padding array of left, top, right, bottom padding
      * @return
      */
@@ -124,7 +159,7 @@
         Graphics2D g = (Graphics2D)graphics2D.create();
         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
-        
+
 
         try {
             if (mPatches.size() == 0) {
@@ -134,30 +169,30 @@
 
             g.translate(x, y);
             x = y = 0;
-            
+
             computePatches(scaledWidth, scaledHeight);
-    
+
             int fixedIndex = 0;
             int horizontalIndex = 0;
             int verticalIndex = 0;
             int patchIndex = 0;
-    
+
             boolean hStretch;
             boolean vStretch;
-    
+
             float vWeightSum = 1.0f;
             float vRemainder = mRemainderVertical;
-    
+
             vStretch = mVerticalStartWithPatch;
             while (y < scaledHeight - 1) {
                 hStretch = mHorizontalStartWithPatch;
-    
+
                 int height = 0;
                 float vExtra = 0.0f;
-    
+
                 float hWeightSum = 1.0f;
                 float hRemainder = mRemainderHorizontal;
-    
+
                 while (x < scaledWidth - 1) {
                     Rectangle r;
                     if (!vStretch) {
@@ -197,7 +232,7 @@
                                     r.x + r.width, r.y + r.height, null);
                             x += r.width;
                         }
-                        
+
                     }
                     hStretch = !hStretch;
                 }
@@ -209,12 +244,12 @@
                 }
                 vStretch = !vStretch;
             }
-    
+
         } finally {
             g.dispose();
         }
     }
-    
+
     void computePatches(int scaledWidth, int scaledHeight) {
         boolean measuredWidth = false;
         boolean endRow = true;
@@ -283,14 +318,13 @@
         }
     }
 
-    
-    private NinePatch(URL fileUrl, BufferedImage image) {
-        mFileUrl = fileUrl;
+
+    private NinePatch(BufferedImage image) {
         mImage = image;
-        
+
         findPatches();
     }
-    
+
     private void findPatches() {
         int width = mImage.getWidth();
         int height = mImage.getHeight();
@@ -462,7 +496,7 @@
 
         return buffer;
     }
-    
+
     static class Pair<E> {
         E mFirst;
         E mSecond;