| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.display; |
| 18 |
|
| 19 |
import android.annotation.FloatRange; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.util.MathUtils; |
| 26 |
import android.util.TypedXmlPullParser; |
| 27 |
import android.util.TypedXmlSerializer; |
| 28 |
|
| 29 |
import com.android.internal.util.XmlUtils; |
| 30 |
|
| 31 |
import org.xmlpull.v1.XmlPullParserException; |
| 32 |
|
| 33 |
import java.io.IOException; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
@SystemApi |
| 46 |
public final class BrightnessCorrection implements Parcelable { |
| 47 |
|
| 48 |
private static final int SCALE_AND_TRANSLATE_LOG = 1; |
| 49 |
|
| 50 |
private static final String TAG_SCALE_AND_TRANSLATE_LOG = "scale-and-translate-log"; |
| 51 |
|
| 52 |
private BrightnessCorrectionImplementation mImplementation; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private BrightnessCorrection(BrightnessCorrectionImplementation implementation) { |
| 57 |
mImplementation = implementation; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@NonNull |
| 76 |
public static BrightnessCorrection createScaleAndTranslateLog(float scale, float translate) { |
| 77 |
BrightnessCorrectionImplementation implementation = |
| 78 |
new ScaleAndTranslateLog(scale, translate); |
| 79 |
return new BrightnessCorrection(implementation); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
@FloatRange(from = 0.0) |
| 91 |
public float apply(@FloatRange(from = 0.0) float brightness) { |
| 92 |
return mImplementation.apply(brightness); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@NonNull |
| 101 |
public String toString() { |
| 102 |
return mImplementation.toString(); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public boolean equals(@Nullable Object o) { |
| 107 |
if (o == this) { |
| 108 |
return true; |
| 109 |
} |
| 110 |
if (!(o instanceof BrightnessCorrection)) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
BrightnessCorrection other = (BrightnessCorrection) o; |
| 114 |
return other.mImplementation.equals(mImplementation); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public int hashCode() { |
| 119 |
return mImplementation.hashCode(); |
| 120 |
} |
| 121 |
|
| 122 |
public static final @android.annotation.NonNull Creator<BrightnessCorrection> CREATOR = |
| 123 |
new Creator<BrightnessCorrection>() { |
| 124 |
public BrightnessCorrection createFromParcel(Parcel in) { |
| 125 |
final int type = in.readInt(); |
| 126 |
switch (type) { |
| 127 |
case SCALE_AND_TRANSLATE_LOG: |
| 128 |
return ScaleAndTranslateLog.readFromParcel(in); |
| 129 |
} |
| 130 |
return null; |
| 131 |
} |
| 132 |
|
| 133 |
public BrightnessCorrection[] newArray(int size) { |
| 134 |
return new BrightnessCorrection[size]; |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
@Override |
| 139 |
public void writeToParcel(Parcel dest, int flags) { |
| 140 |
mImplementation.writeToParcel(dest); |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public int describeContents() { |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void saveToXml(TypedXmlSerializer serializer) throws IOException { |
| 157 |
mImplementation.saveToXml(serializer); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public static BrightnessCorrection loadFromXml(TypedXmlPullParser parser) throws IOException, |
| 174 |
XmlPullParserException { |
| 175 |
final int depth = parser.getDepth(); |
| 176 |
while (XmlUtils.nextElementWithin(parser, depth)) { |
| 177 |
if (TAG_SCALE_AND_TRANSLATE_LOG.equals(parser.getName())) { |
| 178 |
return ScaleAndTranslateLog.loadFromXml(parser); |
| 179 |
} |
| 180 |
} |
| 181 |
return null; |
| 182 |
} |
| 183 |
|
| 184 |
private static float loadFloatFromXml(TypedXmlPullParser parser, String attribute) { |
| 185 |
return parser.getAttributeFloat(null, attribute, Float.NaN); |
| 186 |
} |
| 187 |
|
| 188 |
private interface BrightnessCorrectionImplementation { |
| 189 |
float apply(float brightness); |
| 190 |
String toString(); |
| 191 |
void writeToParcel(Parcel dest); |
| 192 |
void saveToXml(TypedXmlSerializer serializer) throws IOException; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
private static class ScaleAndTranslateLog implements BrightnessCorrectionImplementation { |
| 204 |
private static final float MIN_SCALE = 0.5f; |
| 205 |
private static final float MAX_SCALE = 2.0f; |
| 206 |
private static final float MIN_TRANSLATE = -0.6f; |
| 207 |
private static final float MAX_TRANSLATE = 0.7f; |
| 208 |
|
| 209 |
private static final String ATTR_SCALE = "scale"; |
| 210 |
private static final String ATTR_TRANSLATE = "translate"; |
| 211 |
|
| 212 |
private final float mScale; |
| 213 |
private final float mTranslate; |
| 214 |
|
| 215 |
ScaleAndTranslateLog(float scale, float translate) { |
| 216 |
if (Float.isNaN(scale) || Float.isNaN(translate)) { |
| 217 |
throw new IllegalArgumentException("scale and translate must be numbers"); |
| 218 |
} |
| 219 |
mScale = MathUtils.constrain(scale, MIN_SCALE, MAX_SCALE); |
| 220 |
mTranslate = MathUtils.constrain(translate, MIN_TRANSLATE, MAX_TRANSLATE); |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public float apply(float brightness) { |
| 225 |
return MathUtils.exp(mScale * MathUtils.log(brightness) + mTranslate); |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public String toString() { |
| 230 |
return "ScaleAndTranslateLog(" + mScale + ", " + mTranslate + ")"; |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public boolean equals(@Nullable Object o) { |
| 235 |
if (o == this) { |
| 236 |
return true; |
| 237 |
} |
| 238 |
if (!(o instanceof ScaleAndTranslateLog)) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
ScaleAndTranslateLog other = (ScaleAndTranslateLog) o; |
| 242 |
return other.mScale == mScale && other.mTranslate == mTranslate; |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public int hashCode() { |
| 247 |
int result = 1; |
| 248 |
result = result * 31 + Float.hashCode(mScale); |
| 249 |
result = result * 31 + Float.hashCode(mTranslate); |
| 250 |
return result; |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
public void writeToParcel(Parcel dest) { |
| 255 |
dest.writeInt(SCALE_AND_TRANSLATE_LOG); |
| 256 |
dest.writeFloat(mScale); |
| 257 |
dest.writeFloat(mTranslate); |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public void saveToXml(TypedXmlSerializer serializer) throws IOException { |
| 262 |
serializer.startTag(null, TAG_SCALE_AND_TRANSLATE_LOG); |
| 263 |
serializer.attributeFloat(null, ATTR_SCALE, mScale); |
| 264 |
serializer.attributeFloat(null, ATTR_TRANSLATE, mTranslate); |
| 265 |
serializer.endTag(null, TAG_SCALE_AND_TRANSLATE_LOG); |
| 266 |
} |
| 267 |
|
| 268 |
static BrightnessCorrection readFromParcel(Parcel in) { |
| 269 |
float scale = in.readFloat(); |
| 270 |
float translate = in.readFloat(); |
| 271 |
return BrightnessCorrection.createScaleAndTranslateLog(scale, translate); |
| 272 |
} |
| 273 |
|
| 274 |
static BrightnessCorrection loadFromXml(TypedXmlPullParser parser) throws IOException, |
| 275 |
XmlPullParserException { |
| 276 |
final float scale = loadFloatFromXml(parser, ATTR_SCALE); |
| 277 |
final float translate = loadFloatFromXml(parser, ATTR_TRANSLATE); |
| 278 |
return BrightnessCorrection.createScaleAndTranslateLog(scale, translate); |
| 279 |
} |
| 280 |
} |
| 281 |
} |