Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.ByteArrayOutputStream; |
michael@0 | 8 | import java.io.File; |
michael@0 | 9 | import java.io.FileInputStream; |
michael@0 | 10 | import java.nio.MappedByteBuffer; |
michael@0 | 11 | import java.nio.channels.FileChannel; |
michael@0 | 12 | |
michael@0 | 13 | import android.graphics.Bitmap; |
michael@0 | 14 | import android.util.Base64; |
michael@0 | 15 | import android.util.Base64OutputStream; |
michael@0 | 16 | |
michael@0 | 17 | public class PaintedSurface { |
michael@0 | 18 | private String mFileName; |
michael@0 | 19 | private int mWidth; |
michael@0 | 20 | private int mHeight; |
michael@0 | 21 | private FileInputStream mPixelFile; |
michael@0 | 22 | private MappedByteBuffer mPixelBuffer; |
michael@0 | 23 | |
michael@0 | 24 | public PaintedSurface(String filename, int width, int height) { |
michael@0 | 25 | mFileName = filename; |
michael@0 | 26 | mWidth = width; |
michael@0 | 27 | mHeight = height; |
michael@0 | 28 | |
michael@0 | 29 | try { |
michael@0 | 30 | File f = new File(filename); |
michael@0 | 31 | int pixelSize = (int)f.length(); |
michael@0 | 32 | |
michael@0 | 33 | mPixelFile = new FileInputStream(filename); |
michael@0 | 34 | mPixelBuffer = mPixelFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, pixelSize); |
michael@0 | 35 | } catch (java.io.FileNotFoundException e) { |
michael@0 | 36 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); |
michael@0 | 37 | } catch (java.io.IOException e) { |
michael@0 | 38 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); |
michael@0 | 39 | } |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | public final int getWidth() { |
michael@0 | 43 | return mWidth; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | public final int getHeight() { |
michael@0 | 47 | return mHeight; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | private int pixelAtIndex(int index) { |
michael@0 | 51 | int b1 = mPixelBuffer.get(index) & 0xFF; |
michael@0 | 52 | int b2 = mPixelBuffer.get(index + 1) & 0xFF; |
michael@0 | 53 | int b3 = mPixelBuffer.get(index + 2) & 0xFF; |
michael@0 | 54 | int b4 = mPixelBuffer.get(index + 3) & 0xFF; |
michael@0 | 55 | int value = (b1 << 24) + (b2 << 16) + (b3 << 8) + (b4 << 0); |
michael@0 | 56 | return value; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | public final int getPixelAt(int x, int y) { |
michael@0 | 60 | if (mPixelBuffer == null) { |
michael@0 | 61 | throw new RoboCopException("Trying to access PaintedSurface with no active PixelBuffer"); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | if (x >= mWidth || x < 0) { |
michael@0 | 65 | throw new RoboCopException("Trying to access PaintedSurface with invalid x value"); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | if (y >= mHeight || y < 0) { |
michael@0 | 69 | throw new RoboCopException("Trying to access PaintedSurface with invalid y value"); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | // The rows are reversed so row 0 is at the end and we start with the last row. |
michael@0 | 73 | // This is why we do mHeight-y; |
michael@0 | 74 | int index = (x + ((mHeight - y - 1) * mWidth)) * 4; |
michael@0 | 75 | return pixelAtIndex(index); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | public final String asDataUri() { |
michael@0 | 79 | try { |
michael@0 | 80 | Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); |
michael@0 | 81 | for (int y = 0; y < mHeight; y++) { |
michael@0 | 82 | for (int x = 0; x < mWidth; x++) { |
michael@0 | 83 | int index = (x + ((mHeight - y - 1) * mWidth)) * 4; |
michael@0 | 84 | bm.setPixel(x, y, pixelAtIndex(index)); |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
michael@0 | 88 | out.write("data:image/png;base64,".getBytes()); |
michael@0 | 89 | Base64OutputStream b64 = new Base64OutputStream(out, Base64.NO_WRAP); |
michael@0 | 90 | bm.compress(Bitmap.CompressFormat.PNG, 100, b64); |
michael@0 | 91 | return new String(out.toByteArray()); |
michael@0 | 92 | } catch (Exception e) { |
michael@0 | 93 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); |
michael@0 | 94 | throw new RoboCopException("Unable to convert surface to a PNG data:uri"); |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | public void close() { |
michael@0 | 99 | try { |
michael@0 | 100 | mPixelFile.close(); |
michael@0 | 101 | } catch (Exception e) { |
michael@0 | 102 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, e); |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | } |