1/*
2 * Copyright (C) 2011 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.filterpacks.transform;
18
19// TODO: scale filter needs to be able to specify output width and height
20import androidx.media.filterfw.FrameType;
21import androidx.media.filterfw.InputPort;
22import androidx.media.filterfw.MffContext;
23import androidx.media.filterfw.Signature;
24
25public class ScaleFilter extends ResizeFilter {
26
27    private float mScale = 1.0f;
28
29    public ScaleFilter(MffContext context, String name) {
30        super(context, name);
31    }
32
33    @Override
34    public Signature getSignature() {
35        FrameType imageIn = FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_GPU);
36        FrameType imageOut = FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.WRITE_GPU);
37        return new Signature()
38            .addInputPort("image", Signature.PORT_REQUIRED, imageIn)
39            .addInputPort("scale", Signature.PORT_OPTIONAL, FrameType.single(float.class))
40            .addInputPort("useMipmaps", Signature.PORT_OPTIONAL, FrameType.single(boolean.class))
41            .addOutputPort("image", Signature.PORT_REQUIRED, imageOut)
42            .disallowOtherPorts();
43    }
44
45    @Override
46    public void onInputPortOpen(InputPort port) {
47        if (port.getName().equals("scale")) {
48            port.bindToFieldNamed("mScale");
49            port.setAutoPullEnabled(true);
50        } else if (port.getName().equals("useMipmaps")) {
51            port.bindToFieldNamed("mUseMipmaps");
52            port.setAutoPullEnabled(true);
53        }
54    }
55
56    @Override
57    protected int getOutputWidth(int inWidth, int inHeight) {
58        return (int)(inWidth * mScale);
59    }
60
61    @Override
62    protected int getOutputHeight(int inWidth, int inHeight) {
63        return (int)(inHeight * mScale);
64    }
65
66}
67