Bug 930916 - Add a function for getting the PaintedSurface as a data URI. r=gbrown
authorKartikaya Gupta <kgupta@mozilla.com>
Mon, 28 Oct 2013 09:25:48 -0400
changeset 152634 373338c73fafea12353ff0c69ddcebd7e8b5d245
parent 152474 c5e4bb7db0b9d9ccddeb6088014500b65933094b
child 152635 80ff8dbb228b5821c288465400f6b83e3c3ec4c0
push id35608
push useremorley@mozilla.com
push dateTue, 29 Oct 2013 16:36:07 +0000
treeherdermozilla-inbound@936a5d4b340d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgbrown
bugs930916
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 930916 - Add a function for getting the PaintedSurface as a data URI. r=gbrown
build/mobile/robocop/PaintedSurface.java.in
--- a/build/mobile/robocop/PaintedSurface.java.in
+++ b/build/mobile/robocop/PaintedSurface.java.in
@@ -1,16 +1,21 @@
 #filter substitution
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package @ANDROID_PACKAGE_NAME@;
 
+import android.graphics.Bitmap;
+import android.util.Base64;
+import android.util.Base64OutputStream;
+
 import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.nio.MappedByteBuffer;
 import java.nio.channels.FileChannel;
 
 public class PaintedSurface {
     private String mFileName;
     private int mWidth;
@@ -39,38 +44,62 @@ public class PaintedSurface {
     public final int getWidth() {
         return mWidth;
     }
 
     public final int getHeight() {
         return mHeight;
     }
 
+    private int pixelAtIndex(int index) {
+        int b1 = mPixelBuffer.get(index) & 0xFF;
+        int b2 = mPixelBuffer.get(index + 1) & 0xFF;
+        int b3 = mPixelBuffer.get(index + 2) & 0xFF;
+        int b4 = mPixelBuffer.get(index + 3) & 0xFF;
+        int value = (b1 << 24) + (b2 << 16) + (b3 << 8) + (b4 << 0);
+        return value;
+    }
+
     public final int getPixelAt(int x, int y) {
         if (mPixelBuffer == null) {
             throw new RoboCopException("Trying to access PaintedSurface with no active PixelBuffer");
         }
 
         if (x >= mWidth || x < 0) {
             throw new RoboCopException("Trying to access PaintedSurface with invalid x value");
         }
 
         if (y >= mHeight || y < 0) {
             throw new RoboCopException("Trying to access PaintedSurface with invalid y value");
         }
 
         // The rows are reversed so row 0 is at the end and we start with the last row.
         // This is why we do mHeight-y;
         int index = (x + ((mHeight - y - 1) * mWidth)) * 4;
-        int b1 = mPixelBuffer.get(index) & 0xFF;
-        int b2 = mPixelBuffer.get(index + 1) & 0xFF;
-        int b3 = mPixelBuffer.get(index + 2) & 0xFF;
-        int b4 = mPixelBuffer.get(index + 3) & 0xFF;
-        int value = (b1 << 24) + (b2 << 16) + (b3 << 8) + (b4 << 0);
-        return value;
+        return pixelAtIndex(index);
+    }
+
+    public final String asDataUri() {
+        try {
+            Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
+            for (int y = 0; y < mHeight; y++) {
+                for (int x = 0; x < mWidth; x++) {
+                    int index = (x + ((mHeight - y - 1) * mWidth)) * 4;
+                    bm.setPixel(x, y, pixelAtIndex(index));
+                }
+            }
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            out.write("data:image/png;base64,".getBytes());
+            Base64OutputStream b64 = new Base64OutputStream(out, Base64.NO_WRAP);
+            bm.compress(Bitmap.CompressFormat.PNG, 100, b64);
+            return new String(out.toByteArray());
+        } catch (Exception e) {
+            FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
+            throw new RoboCopException("Unable to convert surface to a PNG data:uri");
+        }
     }
 
     public void close() {
         try {
             mPixelFile.close();
         } catch (Exception e) {
             FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, e);
         }