build/mobile/robocop/PaintedSurface.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/build/mobile/robocop/PaintedSurface.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko;
     1.9 +
    1.10 +import java.io.ByteArrayOutputStream;
    1.11 +import java.io.File;
    1.12 +import java.io.FileInputStream;
    1.13 +import java.nio.MappedByteBuffer;
    1.14 +import java.nio.channels.FileChannel;
    1.15 +
    1.16 +import android.graphics.Bitmap;
    1.17 +import android.util.Base64;
    1.18 +import android.util.Base64OutputStream;
    1.19 +
    1.20 +public class PaintedSurface {
    1.21 +    private String mFileName;
    1.22 +    private int mWidth;
    1.23 +    private int mHeight;
    1.24 +    private FileInputStream mPixelFile;
    1.25 +    private MappedByteBuffer mPixelBuffer;
    1.26 +
    1.27 +    public PaintedSurface(String filename, int width, int height) {
    1.28 +        mFileName = filename;
    1.29 +        mWidth = width;
    1.30 +        mHeight = height;
    1.31 +
    1.32 +        try {
    1.33 +            File f = new File(filename);
    1.34 +            int pixelSize = (int)f.length();
    1.35 +
    1.36 +            mPixelFile = new FileInputStream(filename);
    1.37 +            mPixelBuffer = mPixelFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, pixelSize);
    1.38 +        } catch (java.io.FileNotFoundException e) {
    1.39 +            FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
    1.40 +        } catch (java.io.IOException e) {
    1.41 +            FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
    1.42 +        }
    1.43 +    }
    1.44 +
    1.45 +    public final int getWidth() {
    1.46 +        return mWidth;
    1.47 +    }
    1.48 +
    1.49 +    public final int getHeight() {
    1.50 +        return mHeight;
    1.51 +    }
    1.52 +
    1.53 +    private int pixelAtIndex(int index) {
    1.54 +        int b1 = mPixelBuffer.get(index) & 0xFF;
    1.55 +        int b2 = mPixelBuffer.get(index + 1) & 0xFF;
    1.56 +        int b3 = mPixelBuffer.get(index + 2) & 0xFF;
    1.57 +        int b4 = mPixelBuffer.get(index + 3) & 0xFF;
    1.58 +        int value = (b1 << 24) + (b2 << 16) + (b3 << 8) + (b4 << 0);
    1.59 +        return value;
    1.60 +    }
    1.61 +
    1.62 +    public final int getPixelAt(int x, int y) {
    1.63 +        if (mPixelBuffer == null) {
    1.64 +            throw new RoboCopException("Trying to access PaintedSurface with no active PixelBuffer");
    1.65 +        }
    1.66 +
    1.67 +        if (x >= mWidth || x < 0) {
    1.68 +            throw new RoboCopException("Trying to access PaintedSurface with invalid x value");
    1.69 +        }
    1.70 +
    1.71 +        if (y >= mHeight || y < 0) {
    1.72 +            throw new RoboCopException("Trying to access PaintedSurface with invalid y value");
    1.73 +        }
    1.74 +
    1.75 +        // The rows are reversed so row 0 is at the end and we start with the last row.
    1.76 +        // This is why we do mHeight-y;
    1.77 +        int index = (x + ((mHeight - y - 1) * mWidth)) * 4;
    1.78 +        return pixelAtIndex(index);
    1.79 +    }
    1.80 +
    1.81 +    public final String asDataUri() {
    1.82 +        try {
    1.83 +            Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
    1.84 +            for (int y = 0; y < mHeight; y++) {
    1.85 +                for (int x = 0; x < mWidth; x++) {
    1.86 +                    int index = (x + ((mHeight - y - 1) * mWidth)) * 4;
    1.87 +                    bm.setPixel(x, y, pixelAtIndex(index));
    1.88 +                }
    1.89 +            }
    1.90 +            ByteArrayOutputStream out = new ByteArrayOutputStream();
    1.91 +            out.write("data:image/png;base64,".getBytes());
    1.92 +            Base64OutputStream b64 = new Base64OutputStream(out, Base64.NO_WRAP);
    1.93 +            bm.compress(Bitmap.CompressFormat.PNG, 100, b64);
    1.94 +            return new String(out.toByteArray());
    1.95 +        } catch (Exception e) {
    1.96 +            FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
    1.97 +            throw new RoboCopException("Unable to convert surface to a PNG data:uri");
    1.98 +        }
    1.99 +    }
   1.100 +
   1.101 +    public void close() {
   1.102 +        try {
   1.103 +            mPixelFile.close();
   1.104 +        } catch (Exception e) {
   1.105 +            FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, e);
   1.106 +        }
   1.107 +    }
   1.108 +}

mercurial