michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import java.io.ByteArrayOutputStream; michael@0: import java.io.File; michael@0: import java.io.FileInputStream; michael@0: import java.nio.MappedByteBuffer; michael@0: import java.nio.channels.FileChannel; michael@0: michael@0: import android.graphics.Bitmap; michael@0: import android.util.Base64; michael@0: import android.util.Base64OutputStream; michael@0: michael@0: public class PaintedSurface { michael@0: private String mFileName; michael@0: private int mWidth; michael@0: private int mHeight; michael@0: private FileInputStream mPixelFile; michael@0: private MappedByteBuffer mPixelBuffer; michael@0: michael@0: public PaintedSurface(String filename, int width, int height) { michael@0: mFileName = filename; michael@0: mWidth = width; michael@0: mHeight = height; michael@0: michael@0: try { michael@0: File f = new File(filename); michael@0: int pixelSize = (int)f.length(); michael@0: michael@0: mPixelFile = new FileInputStream(filename); michael@0: mPixelBuffer = mPixelFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, pixelSize); michael@0: } catch (java.io.FileNotFoundException e) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); michael@0: } catch (java.io.IOException e) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); michael@0: } michael@0: } michael@0: michael@0: public final int getWidth() { michael@0: return mWidth; michael@0: } michael@0: michael@0: public final int getHeight() { michael@0: return mHeight; michael@0: } michael@0: michael@0: private int pixelAtIndex(int index) { michael@0: int b1 = mPixelBuffer.get(index) & 0xFF; michael@0: int b2 = mPixelBuffer.get(index + 1) & 0xFF; michael@0: int b3 = mPixelBuffer.get(index + 2) & 0xFF; michael@0: int b4 = mPixelBuffer.get(index + 3) & 0xFF; michael@0: int value = (b1 << 24) + (b2 << 16) + (b3 << 8) + (b4 << 0); michael@0: return value; michael@0: } michael@0: michael@0: public final int getPixelAt(int x, int y) { michael@0: if (mPixelBuffer == null) { michael@0: throw new RoboCopException("Trying to access PaintedSurface with no active PixelBuffer"); michael@0: } michael@0: michael@0: if (x >= mWidth || x < 0) { michael@0: throw new RoboCopException("Trying to access PaintedSurface with invalid x value"); michael@0: } michael@0: michael@0: if (y >= mHeight || y < 0) { michael@0: throw new RoboCopException("Trying to access PaintedSurface with invalid y value"); michael@0: } michael@0: michael@0: // The rows are reversed so row 0 is at the end and we start with the last row. michael@0: // This is why we do mHeight-y; michael@0: int index = (x + ((mHeight - y - 1) * mWidth)) * 4; michael@0: return pixelAtIndex(index); michael@0: } michael@0: michael@0: public final String asDataUri() { michael@0: try { michael@0: Bitmap bm = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); michael@0: for (int y = 0; y < mHeight; y++) { michael@0: for (int x = 0; x < mWidth; x++) { michael@0: int index = (x + ((mHeight - y - 1) * mWidth)) * 4; michael@0: bm.setPixel(x, y, pixelAtIndex(index)); michael@0: } michael@0: } michael@0: ByteArrayOutputStream out = new ByteArrayOutputStream(); michael@0: out.write("data:image/png;base64,".getBytes()); michael@0: Base64OutputStream b64 = new Base64OutputStream(out, Base64.NO_WRAP); michael@0: bm.compress(Bitmap.CompressFormat.PNG, 100, b64); michael@0: return new String(out.toByteArray()); michael@0: } catch (Exception e) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); michael@0: throw new RoboCopException("Unable to convert surface to a PNG data:uri"); michael@0: } michael@0: } michael@0: michael@0: public void close() { michael@0: try { michael@0: mPixelFile.close(); michael@0: } catch (Exception e) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, e); michael@0: } michael@0: } michael@0: }