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 AvgBrightnessFilter extends Filter {
32
33    private static final String TAG = "AvgBrightnessFilter";
34    private static boolean mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE);
35
36    public AvgBrightnessFilter(MffContext context, String name) {
37        super(context, name);
38    }
39
40    @Override
41    public Signature getSignature() {
42        FrameType imageIn = FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_CPU);
43        FrameType floatT = FrameType.single(float.class);
44        return new Signature().addInputPort("image", Signature.PORT_REQUIRED, imageIn)
45                .addOutputPort("brightnessRating", Signature.PORT_OPTIONAL, floatT)
46                .disallowOtherPorts();
47
48    }
49
50    @Override
51    protected void onProcess() {
52        FrameImage2D inputImage = getConnectedInputPort("image").pullFrame().asFrameImage2D();
53
54        float brightness;
55        ByteBuffer inputBuffer  = inputImage.lockBytes(Frame.MODE_READ);
56
57        brightness = brightnessOperator(inputImage.getWidth(),inputImage.getHeight(), inputBuffer);
58
59        inputImage.unlock();
60
61        if (mLogVerbose) Log.v(TAG, "contrastRatio: " + brightness);
62
63        OutputPort brightnessPort = getConnectedOutputPort("brightnessRating");
64        FrameValue brightnessOutFrame = brightnessPort.fetchAvailableFrame(null).asFrameValue();
65        brightnessOutFrame.setValue(brightness);
66        brightnessPort.pushFrame(brightnessOutFrame);
67    }
68
69    private static native float brightnessOperator(int width, int height, ByteBuffer imageBuffer);
70
71    static {
72        System.loadLibrary("smartcamera_jni");
73    }
74
75}
76