BitmapSource.java revision 65953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9
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
17
18package android.filterpacks.imageproc;
19
20import android.content.Context;
21import android.filterfw.core.Filter;
22import android.filterfw.core.FilterContext;
23import android.filterfw.core.Frame;
24import android.filterfw.core.FrameFormat;
25import android.filterfw.core.FrameManager;
26import android.filterfw.core.GenerateFinalPort;
27import android.filterfw.core.GenerateFieldPort;
28import android.filterfw.core.KeyValueMap;
29import android.filterfw.core.MutableFrameFormat;
30import android.filterfw.core.NativeFrame;
31import android.filterfw.format.ImageFormat;
32import android.graphics.Bitmap;
33
34/**
35 * @hide
36 */
37public class BitmapSource extends Filter {
38
39    @GenerateFieldPort(name = "target")
40    String mTargetString;
41
42    @GenerateFieldPort(name = "bitmap")
43    private Bitmap mBitmap;
44
45    @GenerateFieldPort(name = "recycleBitmap", hasDefault = true)
46    private boolean mRecycleBitmap = true;
47
48    @GenerateFieldPort(name = "repeatFrame", hasDefault = true)
49    boolean mRepeatFrame = false;
50
51    private int mTarget;
52    private Frame mImageFrame;
53
54    public BitmapSource(String name) {
55        super(name);
56    }
57
58
59    @Override
60    public void setupPorts() {
61        // Setup output format
62        FrameFormat outputFormat = ImageFormat.create(ImageFormat.COLORSPACE_RGBA,
63                                                      FrameFormat.TARGET_UNSPECIFIED);
64
65        // Add output port
66        addOutputPort("image", outputFormat);
67    }
68
69    public void loadImage(FilterContext filterContext) {
70        // Create frame with bitmap
71        mTarget = FrameFormat.readTargetString(mTargetString);
72        FrameFormat outputFormat = ImageFormat.create(mBitmap.getWidth(),
73                                                      mBitmap.getHeight(),
74                                                      ImageFormat.COLORSPACE_RGBA,
75                                                      mTarget);
76        mImageFrame = filterContext.getFrameManager().newFrame(outputFormat);
77        mImageFrame.setBitmap(mBitmap);
78        mImageFrame.setTimestamp(Frame.TIMESTAMP_UNKNOWN);
79
80        // Free up space used by bitmap
81        if (mRecycleBitmap) {
82            mBitmap.recycle();
83        }
84        mBitmap = null;
85    }
86
87    @Override
88    public void fieldPortValueUpdated(String name, FilterContext context) {
89        // Clear image (to trigger reload) in case parameters have been changed
90        if (name.equals("bitmap") || name.equals("target")) {
91            if (mImageFrame != null) {
92                mImageFrame.release();
93                mImageFrame = null;
94            }
95        }
96    }
97
98    @Override
99    public void process(FilterContext context) {
100        if (mImageFrame == null) {
101            loadImage(context);
102        }
103
104        pushOutput("image", mImageFrame);
105
106        if (!mRepeatFrame) {
107            closeOutputPort("image");
108        }
109    }
110
111    @Override
112    public void tearDown(FilterContext env) {
113        if (mImageFrame != null) {
114            mImageFrame.release();
115            mImageFrame = null;
116        }
117    }
118}
119