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