1/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.media.filterfw.samples.simplecamera;
18
19import android.util.Log;
20import androidx.media.filterfw.Filter;
21import androidx.media.filterfw.Frame;
22import androidx.media.filterfw.FrameImage2D;
23import androidx.media.filterfw.FrameType;
24import androidx.media.filterfw.FrameValue;
25import androidx.media.filterfw.MffContext;
26import androidx.media.filterfw.OutputPort;
27import androidx.media.filterfw.Signature;
28
29import java.nio.ByteBuffer;
30
31public class ExposureFilter extends Filter {
32
33    private FrameType mImageType;
34    private static final String TAG = "ExposureFilter";
35    private static boolean mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);
36    private static int OVER_EXPOSURE_TOLERANCE = 5;
37
38    public ExposureFilter(MffContext context, String name) {
39        super(context, name);
40    }
41
42    @Override
43    public Signature getSignature() {
44        FrameType imageIn = FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_GPU);
45        FrameType floatT = FrameType.single(float.class);
46        return new Signature().addInputPort("image", Signature.PORT_REQUIRED, imageIn)
47                .addOutputPort("overExposedNum", Signature.PORT_OPTIONAL, floatT)
48                .addOutputPort("overExposureRating", Signature.PORT_REQUIRED, floatT)
49                .addOutputPort("underExposedNum", Signature.PORT_OPTIONAL, floatT)
50                .addOutputPort("underExposureRating", Signature.PORT_REQUIRED, floatT)
51                .disallowOtherPorts();
52
53    }
54
55    @Override
56    protected void onProcess() {
57        FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
58
59        float overExposedPixels, underExposedPixels;
60        ByteBuffer inputBuffer = inputImage.lockBytes(Frame.MODE_READ);
61
62        overExposedPixels = overExposureOperator(inputImage.getWidth(),
63                                                 inputImage.getHeight(),
64                                                 inputBuffer);
65        underExposedPixels = underExposureOperator(inputImage.getWidth(),
66                                                   inputImage.getHeight(),
67                                                   inputBuffer);
68        inputImage.unlock();
69
70
71        if (mLogVerbose) Log.v(TAG, "underExposedPixelCount: " + underExposedPixels);
72
73        OutputPort underPort = getConnectedOutputPort("underExposedNum");
74        if (underPort != null) {
75            FrameValue underOutFrame = underPort.fetchAvailableFrame(null).asFrameValue();
76            underOutFrame.setValue(underExposedPixels*inputImage.getWidth()*inputImage.getHeight());
77            underPort.pushFrame(underOutFrame);
78        }
79
80
81        OutputPort underPort2 = getConnectedOutputPort("underExposureRating");
82        FrameValue underOutFrame2 = underPort2.fetchAvailableFrame(null).asFrameValue();
83        underOutFrame2.setValue(underExposedPixels);
84        underPort2.pushFrame(underOutFrame2);
85
86        if (mLogVerbose) Log.v(TAG, "overExposedPixelCount: " + overExposedPixels);
87
88        OutputPort overPort = getConnectedOutputPort("overExposedNum");
89        if (overPort != null) {
90            FrameValue overOutFrame = overPort.fetchAvailableFrame(null).asFrameValue();
91            overOutFrame.setValue(overExposedPixels*inputImage.getWidth()*inputImage.getHeight());
92            overPort.pushFrame(overOutFrame);
93        }
94
95
96        OutputPort overPort2 = getConnectedOutputPort("overExposureRating");
97        FrameValue overOutFrame2 = overPort2.fetchAvailableFrame(null).asFrameValue();
98        overOutFrame2.setValue(overExposedPixels);
99        overPort2.pushFrame(overOutFrame2);
100
101    }
102
103    private static native float overExposureOperator(int width, int height,
104            ByteBuffer imageBuffer);
105    private static native float underExposureOperator(int width, int height,
106            ByteBuffer imageBuffer);
107
108    static {
109        System.loadLibrary("smartcamera_jni");
110    }
111}
112