1 /* 2 * Copyright (C) 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 17 package com.android.gallery3d.filtershow.pipeline; 18 19 import android.graphics.Bitmap; 20 import android.graphics.Canvas; 21 22 import com.android.gallery3d.app.Log; 23 import com.android.gallery3d.filtershow.cache.BitmapCache; 24 import com.android.gallery3d.filtershow.filters.FilterRepresentation; 25 import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation; 26 import com.android.gallery3d.filtershow.filters.FiltersManagerInterface; 27 import com.android.gallery3d.filtershow.filters.ImageFilter; 28 29 import java.lang.ref.WeakReference; 30 import java.util.HashMap; 31 32 public class FilterEnvironment { 33 private static final String LOGTAG = "FilterEnvironment"; 34 private ImagePreset mImagePreset; 35 private float mScaleFactor; 36 private int mQuality; 37 private FiltersManagerInterface mFiltersManager; 38 private PipelineInterface mPipeline; 39 private volatile boolean mStop = false; 40 private BitmapCache mBitmapCache; 41 42 public static final int QUALITY_ICON = 0; 43 public static final int QUALITY_PREVIEW = 1; 44 public static final int QUALITY_FINAL = 2; 45 needsStop()46 public synchronized boolean needsStop() { 47 return mStop; 48 } 49 setStop(boolean stop)50 public synchronized void setStop(boolean stop) { 51 this.mStop = stop; 52 } 53 54 private HashMap<Integer, Integer> 55 generalParameters = new HashMap<Integer, Integer>(); 56 setBitmapCache(BitmapCache cache)57 public void setBitmapCache(BitmapCache cache) { 58 mBitmapCache = cache; 59 } 60 cache(Buffer buffer)61 public void cache(Buffer buffer) { 62 mBitmapCache.cache(buffer); 63 } 64 cache(Bitmap bitmap)65 public void cache(Bitmap bitmap) { 66 mBitmapCache.cache(bitmap); 67 } 68 getBitmap(int w, int h, int type)69 public Bitmap getBitmap(int w, int h, int type) { 70 return mBitmapCache.getBitmap(w, h, type); 71 } 72 getBitmapCopy(Bitmap source, int type)73 public Bitmap getBitmapCopy(Bitmap source, int type) { 74 return mBitmapCache.getBitmapCopy(source, type); 75 } 76 setImagePreset(ImagePreset imagePreset)77 public void setImagePreset(ImagePreset imagePreset) { 78 mImagePreset = imagePreset; 79 } 80 getImagePreset()81 public ImagePreset getImagePreset() { 82 return mImagePreset; 83 } 84 setScaleFactor(float scaleFactor)85 public void setScaleFactor(float scaleFactor) { 86 mScaleFactor = scaleFactor; 87 } 88 getScaleFactor()89 public float getScaleFactor() { 90 return mScaleFactor; 91 } 92 setQuality(int quality)93 public void setQuality(int quality) { 94 mQuality = quality; 95 } 96 getQuality()97 public int getQuality() { 98 return mQuality; 99 } 100 setFiltersManager(FiltersManagerInterface filtersManager)101 public void setFiltersManager(FiltersManagerInterface filtersManager) { 102 mFiltersManager = filtersManager; 103 } 104 getFiltersManager()105 public FiltersManagerInterface getFiltersManager() { 106 return mFiltersManager; 107 } 108 applyRepresentation(FilterRepresentation representation, Bitmap bitmap)109 public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) { 110 if (representation instanceof FilterUserPresetRepresentation) { 111 // we allow instances of FilterUserPresetRepresentation in a preset only to know if one 112 // has been applied (so we can show this in the UI). But as all the filters in them are 113 // applied directly they do not themselves need to do any kind of filtering. 114 return bitmap; 115 } 116 ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation); 117 if (filter == null){ 118 Log.e(LOGTAG,"No ImageFilter for "+representation.getSerializationName()); 119 } 120 filter.useRepresentation(representation); 121 filter.setEnvironment(this); 122 Bitmap ret = filter.apply(bitmap, mScaleFactor, mQuality); 123 if (bitmap != ret) { 124 cache(bitmap); 125 } 126 filter.setGeneralParameters(); 127 filter.setEnvironment(null); 128 return ret; 129 } 130 getPipeline()131 public PipelineInterface getPipeline() { 132 return mPipeline; 133 } 134 setPipeline(PipelineInterface cachingPipeline)135 public void setPipeline(PipelineInterface cachingPipeline) { 136 mPipeline = cachingPipeline; 137 } 138 clearGeneralParameters()139 public synchronized void clearGeneralParameters() { 140 generalParameters = null; 141 } 142 getGeneralParameter(int id)143 public synchronized Integer getGeneralParameter(int id) { 144 if (generalParameters == null || !generalParameters.containsKey(id)) { 145 return null; 146 } 147 return generalParameters.get(id); 148 } 149 setGeneralParameter(int id, int value)150 public synchronized void setGeneralParameter(int id, int value) { 151 if (generalParameters == null) { 152 generalParameters = new HashMap<Integer, Integer>(); 153 } 154 155 generalParameters.put(id, value); 156 } 157 getBimapCache()158 public BitmapCache getBimapCache() { 159 return mBitmapCache; 160 } 161 } 162