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.filterfw.core; 19 20import android.filterfw.core.Frame; 21import android.filterfw.core.FrameFormat; 22import android.filterfw.core.FrameManager; 23import android.filterfw.format.ObjectFormat; 24import android.graphics.Bitmap; 25 26import java.nio.ByteBuffer; 27 28/** 29 * @hide 30 */ 31public class SimpleFrame extends Frame { 32 33 private Object mObject; 34 35 SimpleFrame(FrameFormat format, FrameManager frameManager) { 36 super(format, frameManager); 37 initWithFormat(format); 38 setReusable(false); 39 } 40 41 static SimpleFrame wrapObject(Object object, FrameManager frameManager) { 42 FrameFormat format = ObjectFormat.fromObject(object, FrameFormat.TARGET_SIMPLE); 43 SimpleFrame result = new SimpleFrame(format, frameManager); 44 result.setObjectValue(object); 45 return result; 46 } 47 48 private void initWithFormat(FrameFormat format) { 49 final int count = format.getLength(); 50 final int baseType = format.getBaseType(); 51 switch (baseType) { 52 case FrameFormat.TYPE_BYTE: 53 mObject = new byte[count]; 54 break; 55 case FrameFormat.TYPE_INT16: 56 mObject = new short[count]; 57 break; 58 case FrameFormat.TYPE_INT32: 59 mObject = new int[count]; 60 break; 61 case FrameFormat.TYPE_FLOAT: 62 mObject = new float[count]; 63 break; 64 case FrameFormat.TYPE_DOUBLE: 65 mObject = new double[count]; 66 break; 67 default: 68 mObject = null; 69 break; 70 } 71 } 72 73 @Override 74 protected boolean hasNativeAllocation() { 75 return false; 76 } 77 78 @Override 79 protected void releaseNativeAllocation() { 80 } 81 82 @Override 83 public Object getObjectValue() { 84 return mObject; 85 } 86 87 @Override 88 public void setInts(int[] ints) { 89 assertFrameMutable(); 90 setGenericObjectValue(ints); 91 } 92 93 @Override 94 public int[] getInts() { 95 return (mObject instanceof int[]) ? (int[])mObject : null; 96 } 97 98 @Override 99 public void setFloats(float[] floats) { 100 assertFrameMutable(); 101 setGenericObjectValue(floats); 102 } 103 104 @Override 105 public float[] getFloats() { 106 return (mObject instanceof float[]) ? (float[])mObject : null; 107 } 108 109 @Override 110 public void setData(ByteBuffer buffer, int offset, int length) { 111 assertFrameMutable(); 112 setGenericObjectValue(ByteBuffer.wrap(buffer.array(), offset, length)); 113 } 114 115 @Override 116 public ByteBuffer getData() { 117 return (mObject instanceof ByteBuffer) ? (ByteBuffer)mObject : null; 118 } 119 120 @Override 121 public void setBitmap(Bitmap bitmap) { 122 assertFrameMutable(); 123 setGenericObjectValue(bitmap); 124 } 125 126 @Override 127 public Bitmap getBitmap() { 128 return (mObject instanceof Bitmap) ? (Bitmap)mObject : null; 129 } 130 131 private void setFormatObjectClass(Class objectClass) { 132 MutableFrameFormat format = getFormat().mutableCopy(); 133 format.setObjectClass(objectClass); 134 setFormat(format); 135 } 136 137 @Override 138 protected void setGenericObjectValue(Object object) { 139 // Update the FrameFormat class 140 // TODO: Take this out! FrameFormats should not be modified and convenience formats used 141 // instead! 142 FrameFormat format = getFormat(); 143 if (format.getObjectClass() == null) { 144 setFormatObjectClass(object.getClass()); 145 } else if (!format.getObjectClass().isAssignableFrom(object.getClass())) { 146 throw new RuntimeException( 147 "Attempting to set object value of type '" + object.getClass() + "' on " + 148 "SimpleFrame of type '" + format.getObjectClass() + "'!"); 149 } 150 151 // Set the object value 152 mObject = object; 153 } 154 155 @Override 156 public String toString() { 157 return "SimpleFrame (" + getFormat() + ")"; 158 } 159} 160