summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/util/ClearSightNativeEngine.java
blob: cf0a740d7eb887f2a8e72e283ae8b89d26ee5be6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *  * Neither the name of The Linux Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.camera.util;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;

import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.media.Image;
import android.media.Image.Plane;
import android.util.Log;

public class ClearSightNativeEngine {
    private static final String TAG = "ClearSightNativeEngine";
    static {
        try {
            System.loadLibrary("jni_clearsight");
            mLibLoaded = true;
            Log.v(TAG, "successfully loaded clearsight lib");
        } catch (UnsatisfiedLinkError e) {
            Log.e(TAG, "failed to load clearsight lib");
            e.printStackTrace();
            mLibLoaded = false;
        }
    }

    private static final int METADATA_SIZE = 5;
    private static final int Y_PLANE = 0;
    private static final int VU_PLANE = 2;

    // dummy OTP calib data
    private static final String otp_calib = "Calibration OTP format version = 10301\n"
            + "Main Native Sensor Resolution width = 4224px\n"
            + "Main Native Sensor Resolution height = 3136px\n"
            + "Main Calibration Resolution width = 1280px\n"
            + "Main Calibration Resolution height = 950px\n"
            + "Main Focal length ratio = 1.004896\n"
            + "Aux Native Sensor Resolution width = 1600px\n"
            + "Aux Native Sensor Resolution height = 1200px\n"
            + "Aux Calibration Resolution width = 1280px\n"
            + "Aux Calibration Resolution height = 960px\n"
            + "Aux Focal length ratio = 1.000000\n"
            + "Relative Rotation matrix [0] through [8] = 1.000000,-0.003008,0.000251,0.003073,1.000189,0.003329,0.019673,-0.003329,1.000284\n"
            + "Relative Geometric surface parameters [0] through [31] = -0.307164,-0.879074,4.636152,0.297486,-0.157539,-6.889396,0.109467,-2.797022,-0.066306,-0.120142,0.196464,0.021974,2.905827,0.241197,0.048328,-5.116615,0.496533,-5.263813,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000\n"
            + "Relative Principal point X axis offset (ox) = 0.000000px\n"
            + "Relative Principal point Y axis offset (oy) = 0.000000px\n"
            + "Relative position flag = 1\n"
            + "Baseline distance = 20.000000mm\n"
            + "Main sensor mirror and flip setting = 3\n"
            + "Aux sensor mirror and flip setting = 3\n"
            + "Module orientation during calibration = 0\n"
            + "Rotation flag = 0\n"
            + "Main Normalized Focal length = 1000.0px\n"
            + "Aux Normalized Focal length = 1000.0px";

    private static boolean mLibLoaded;
    private static ClearSightNativeEngine mInstance;

    private Image mRefColorImage;
    private Image mRefMonoImage;
    private ArrayList<SourceImage> mSrcColor = new ArrayList<SourceImage>();
    private ArrayList<SourceImage> mSrcMono = new ArrayList<SourceImage>();

    private ClearSightNativeEngine() {
    }

    public static void createInstance() {
        if (mInstance == null) {
            mInstance = new ClearSightNativeEngine();
        }
    }

    public static ClearSightNativeEngine getInstance() {
        createInstance();
        return mInstance;
    }

    public boolean isLibLoaded() {
        return mLibLoaded;
    }

    public void reset() {
        mSrcColor.clear();
        mSrcMono.clear();
        setReferenceColorImage(null);
        setReferenceMonoImage(null);
    }

    public void setReferenceImage(boolean color, Image image) {
        if (color)
            setReferenceColorImage(image);
        else
            setReferenceMonoImage(image);
    }

    private void setReferenceColorImage(Image reference) {
        if (mRefColorImage != null) {
            mRefColorImage.close();
            mRefColorImage = null;
        }

        mRefColorImage = reference;

        if (mRefColorImage != null) {
            Log.e(TAG,
                    "setRefColorImage - isdirectbuff: "
                            + mRefColorImage.getPlanes()[0].getBuffer()
                                    .isDirect());
            mSrcColor.add(new SourceImage(mRefColorImage.getPlanes()[Y_PLANE]
                    .getBuffer(), mRefColorImage.getPlanes()[VU_PLANE]
                    .getBuffer(), new int[] { 0, 0, 0, 0, 0 }));
        }
    }

    private void setReferenceMonoImage(Image reference) {
        if (mRefMonoImage != null) {
            mRefMonoImage.close();
            mRefMonoImage = null;
        }

        mRefMonoImage = reference;

        if (mRefMonoImage != null) {
            Log.e(TAG,
                    "setRefMonoImage - isdirectbuff: "
                            + mRefMonoImage.getPlanes()[0].getBuffer()
                                    .isDirect());
            mSrcMono.add(new SourceImage(mRefMonoImage.getPlanes()[Y_PLANE]
                    .getBuffer(), null, new int[] { 0, 0, 0, 0, 0 }));
        }
    }

    public boolean hasReferenceImage(boolean color) {
        return !(color ? mSrcColor.isEmpty() : mSrcMono.isEmpty());
    }

    public Image getReferenceImage(boolean color) {
        return color ? mRefColorImage : mRefMonoImage;
    }

    public boolean registerImage(boolean color, Image image) {
        return (color ? registerColorImage(image) : registerMonoImage(image));
    }

    private boolean registerColorImage(Image image) {
        if (mSrcColor.isEmpty()) {
            Log.w(TAG, "reference color image not yet set");
            return false;
        }

        Plane[] planes = image.getPlanes();
        ByteBuffer refY = mRefColorImage.getPlanes()[Y_PLANE].getBuffer();
        ByteBuffer refVU = mRefColorImage.getPlanes()[VU_PLANE].getBuffer();
        ByteBuffer regY = ByteBuffer.allocateDirect(refY.capacity());
        ByteBuffer regVU = ByteBuffer.allocateDirect(refVU.capacity());
        int[] metadata = new int[METADATA_SIZE];

        boolean result = clearSightRegisterImage(refY,
                planes[Y_PLANE].getBuffer(), planes[VU_PLANE].getBuffer(),
                image.getWidth(), image.getHeight(),
                planes[Y_PLANE].getRowStride(),
                planes[VU_PLANE].getRowStride(), regY, regVU, metadata);

        if (result) {
            mSrcColor.add(new SourceImage(regY, regVU, metadata));
        }

        image.close();
        return result;
    }

    private boolean registerMonoImage(Image image) {
        if (mSrcMono.isEmpty()) {
            Log.w(TAG, "reference mono image not yet set");
            return false;
        }

        Plane[] planes = image.getPlanes();
        ByteBuffer refY = mRefMonoImage.getPlanes()[Y_PLANE].getBuffer();
        ByteBuffer regY = ByteBuffer.allocateDirect(refY.capacity());
        int[] metadata = new int[METADATA_SIZE];

        boolean result = clearSightRegisterImage(refY,
                planes[Y_PLANE].getBuffer(), null, image.getWidth(),
                image.getHeight(), planes[Y_PLANE].getRowStride(), 0, regY,
                null, metadata);

        if (result) {
            mSrcMono.add(new SourceImage(regY, null, metadata));
        }

        image.close();
        return result;
    }

    public ClearsightImage processImage() {
        // check data validity
        if (mSrcColor.size() != mSrcMono.size()) {
            // mis-match in num images
            Log.e(TAG, "processImage - numImages mismatch - bayer: "
                    + mSrcColor.size() + ", mono: " + mSrcMono.size());
            return null;
        }

        int numImages = mSrcColor.size();
        ByteBuffer[] srcColorY = new ByteBuffer[numImages];
        ByteBuffer[] srcColorVU = new ByteBuffer[numImages];
        int[][] metadataColor = new int[numImages][];
        ByteBuffer[] srcMonoY = new ByteBuffer[numImages];
        int[][] metadataMono = new int[numImages][];

        Log.e(TAG, "processImage - numImages: " + numImages);

        for (int i = 0; i < numImages; i++) {
            SourceImage color = mSrcColor.get(i);
            SourceImage mono = mSrcMono.get(i);

            srcColorY[i] = color.mY;
            srcColorVU[i] = color.mVU;
            metadataColor[i] = color.mMetadata;

            srcMonoY[i] = mono.mY;
            metadataMono[i] = mono.mMetadata;
        }

        Plane[] colorPlanes = mRefColorImage.getPlanes();
        Plane[] monoPlanes = mRefMonoImage.getPlanes();
        ByteBuffer dstY = ByteBuffer.allocateDirect(colorPlanes[Y_PLANE]
                .getBuffer().capacity());
        ByteBuffer dstVU = ByteBuffer.allocateDirect(colorPlanes[VU_PLANE]
                .getBuffer().capacity());
        int[] roiRect = new int[4];

        boolean result = clearSightProcess(numImages, srcColorY, srcColorVU,
                metadataColor, mRefColorImage.getWidth(),
                mRefColorImage.getHeight(),
                colorPlanes[Y_PLANE].getRowStride(),
                colorPlanes[VU_PLANE].getRowStride(), srcMonoY, metadataMono,
                mRefMonoImage.getWidth(), mRefMonoImage.getHeight(),
                monoPlanes[Y_PLANE].getRowStride(), otp_calib.getBytes(), dstY, dstVU,
                colorPlanes[Y_PLANE].getRowStride(),
                colorPlanes[VU_PLANE].getRowStride(), roiRect);

        if (result) {
            dstY.rewind();
            dstVU.rewind();
            byte[] data = new byte[dstY.capacity() + dstVU.capacity()];
            int[] strides = new int[] { colorPlanes[Y_PLANE].getRowStride(),
                    colorPlanes[VU_PLANE].getRowStride() };
            dstY.get(data, 0, dstY.capacity());
            dstVU.get(data, dstY.capacity(), dstVU.capacity());
            return new ClearsightImage(new YuvImage(data, ImageFormat.NV21,
                    mRefColorImage.getWidth(), mRefColorImage.getHeight(),
                    strides), roiRect);
        } else {
            return null;
        }
    }

    native public boolean configureClearSight(float focalLengthRatio,
            float brIntensity, float sharpenIntensity);

    native public boolean clearSightRegisterImage(ByteBuffer refY,
            ByteBuffer srcY, ByteBuffer srcVU, int width, int height,
            int strideY, int strideVU, ByteBuffer dstY, ByteBuffer dstVU,
            int[] metadata);

    native public boolean clearSightProcess(int numImagePairs,
            ByteBuffer[] srcColorY, ByteBuffer[] srcColorVU,
            int[][] metadataColor, int srcColorWidth, int srcColorHeight,
            int srcColorStrideY, int srcColorStrideVU, ByteBuffer[] srcMonoY,
            int[][] metadataMono, int srcMonoWidth, int srcMonoHeight,
            int srcMonoStrideY, byte[] otp, ByteBuffer dstY, ByteBuffer dstVU,
            int dstStrideY, int dstStrideVU, int[] roiRect);

    private class SourceImage {
        ByteBuffer mY;
        ByteBuffer mVU;
        int[] mMetadata;

        SourceImage(ByteBuffer y, ByteBuffer vu, int[] metadata) {
            mY = y;
            mVU = vu;
            mMetadata = metadata;
        }
    }

    public static class ClearsightImage {
        private YuvImage mImage;
        private Rect mRoiRect;

        ClearsightImage(YuvImage image, int[] rect) {
            mImage = image;
            mRoiRect = new Rect(rect[0], rect[1], rect[0] + rect[2], rect[1]
                    + rect[3]);
        }

        public Rect getRoiRect() {
            return mRoiRect;
        }

        public long getDataLength() {
            return (mImage==null?0:mImage.getYuvData().length);
        }

        public int getWidth() {
            return (mRoiRect.right - mRoiRect.left);
        }

        public int getHeight() {
            return (mRoiRect.bottom - mRoiRect.top);
        }

        public byte[] compressToJpeg() {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            mImage.compressToJpeg(mRoiRect, 100, baos);
            return baos.toByteArray();
        }
    }
}