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