| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.camera2.params; |
| 18 |
|
| 19 |
import static com.android.internal.util.Preconditions.*; |
| 20 |
|
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.graphics.PointF; |
| 23 |
import android.hardware.camera2.CameraCharacteristics; |
| 24 |
import android.hardware.camera2.CameraDevice; |
| 25 |
import android.hardware.camera2.CameraMetadata; |
| 26 |
import android.hardware.camera2.CaptureRequest; |
| 27 |
import android.hardware.camera2.utils.HashCodeHelpers; |
| 28 |
|
| 29 |
import java.util.Arrays; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public final class TonemapCurve { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static final float LEVEL_BLACK = 0.0f; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static final float LEVEL_WHITE = 1.0f; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static final int POINT_SIZE = 2; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static final int CHANNEL_RED = 0; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static final int CHANNEL_GREEN = 1; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static final int CHANNEL_BLUE = 2; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public TonemapCurve(float[] red, float[] green, float[] blue) { |
| 106 |
|
| 107 |
|
| 108 |
checkNotNull(red, "red must not be null"); |
| 109 |
checkNotNull(green, "green must not be null"); |
| 110 |
checkNotNull(blue, "blue must not be null"); |
| 111 |
|
| 112 |
checkArgumentArrayLengthDivisibleBy(red, POINT_SIZE, "red"); |
| 113 |
checkArgumentArrayLengthDivisibleBy(green, POINT_SIZE, "green"); |
| 114 |
checkArgumentArrayLengthDivisibleBy(blue, POINT_SIZE, "blue"); |
| 115 |
|
| 116 |
checkArgumentArrayLengthNoLessThan(red, MIN_CURVE_LENGTH, "red"); |
| 117 |
checkArgumentArrayLengthNoLessThan(green, MIN_CURVE_LENGTH, "green"); |
| 118 |
checkArgumentArrayLengthNoLessThan(blue, MIN_CURVE_LENGTH, "blue"); |
| 119 |
|
| 120 |
checkArrayElementsInRange(red, LEVEL_BLACK, LEVEL_WHITE, "red"); |
| 121 |
checkArrayElementsInRange(green, LEVEL_BLACK, LEVEL_WHITE, "green"); |
| 122 |
checkArrayElementsInRange(blue, LEVEL_BLACK, LEVEL_WHITE, "blue"); |
| 123 |
|
| 124 |
mRed = Arrays.copyOf(red, red.length); |
| 125 |
mGreen = Arrays.copyOf(green, green.length); |
| 126 |
mBlue = Arrays.copyOf(blue, blue.length); |
| 127 |
} |
| 128 |
|
| 129 |
private static void checkArgumentArrayLengthDivisibleBy(float[] array, |
| 130 |
int divisible, String arrayName) { |
| 131 |
if (array.length % divisible != 0) { |
| 132 |
throw new IllegalArgumentException(arrayName + " size must be divisible by " |
| 133 |
+ divisible); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private static int checkArgumentColorChannel(int colorChannel) { |
| 138 |
switch (colorChannel) { |
| 139 |
case CHANNEL_RED: |
| 140 |
case CHANNEL_GREEN: |
| 141 |
case CHANNEL_BLUE: |
| 142 |
break; |
| 143 |
default: |
| 144 |
throw new IllegalArgumentException("colorChannel out of range"); |
| 145 |
} |
| 146 |
|
| 147 |
return colorChannel; |
| 148 |
} |
| 149 |
|
| 150 |
private static void checkArgumentArrayLengthNoLessThan(float[] array, int minLength, |
| 151 |
String arrayName) { |
| 152 |
if (array.length < minLength) { |
| 153 |
throw new IllegalArgumentException(arrayName + " size must be at least " |
| 154 |
+ minLength); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public int getPointCount(int colorChannel) { |
| 167 |
checkArgumentColorChannel(colorChannel); |
| 168 |
|
| 169 |
return getCurve(colorChannel).length / POINT_SIZE; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public PointF getPoint(int colorChannel, int index) { |
| 191 |
checkArgumentColorChannel(colorChannel); |
| 192 |
if (index < 0 || index >= getPointCount(colorChannel)) { |
| 193 |
throw new IllegalArgumentException("index out of range"); |
| 194 |
} |
| 195 |
|
| 196 |
final float[] curve = getCurve(colorChannel); |
| 197 |
|
| 198 |
final float pIn = curve[index * POINT_SIZE + OFFSET_POINT_IN]; |
| 199 |
final float pOut = curve[index * POINT_SIZE + OFFSET_POINT_OUT]; |
| 200 |
|
| 201 |
return new PointF(pIn, pOut); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public void copyColorCurve(int colorChannel, float[] destination, |
| 232 |
int offset) { |
| 233 |
checkArgumentNonnegative(offset, "offset must not be negative"); |
| 234 |
checkNotNull(destination, "destination must not be null"); |
| 235 |
|
| 236 |
if (destination.length + offset < getPointCount(colorChannel) * POINT_SIZE) { |
| 237 |
throw new ArrayIndexOutOfBoundsException("destination too small to fit elements"); |
| 238 |
} |
| 239 |
|
| 240 |
float[] curve = getCurve(colorChannel); |
| 241 |
System.arraycopy(curve, 0, destination, offset, curve.length); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
@Override |
| 253 |
public boolean equals(@Nullable Object obj) { |
| 254 |
if (obj == null) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
if (this == obj) { |
| 258 |
return true; |
| 259 |
} |
| 260 |
if (obj instanceof TonemapCurve) { |
| 261 |
final TonemapCurve other = (TonemapCurve) obj; |
| 262 |
return Arrays.equals(mRed, other.mRed) && |
| 263 |
Arrays.equals(mGreen, other.mGreen) && |
| 264 |
Arrays.equals(mBlue, other.mBlue); |
| 265 |
} |
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
@Override |
| 273 |
public int hashCode() { |
| 274 |
if (mHashCalculated) { |
| 275 |
|
| 276 |
return mHashCode; |
| 277 |
} |
| 278 |
|
| 279 |
mHashCode = HashCodeHelpers.hashCodeGeneric(mRed, mGreen, mBlue); |
| 280 |
mHashCalculated = true; |
| 281 |
|
| 282 |
return mHashCode; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
@Override |
| 296 |
public String toString() { |
| 297 |
StringBuilder sb = new StringBuilder("TonemapCurve{"); |
| 298 |
sb.append("R:"); |
| 299 |
sb.append(curveToString(CHANNEL_RED)); |
| 300 |
sb.append(", G:"); |
| 301 |
sb.append(curveToString(CHANNEL_GREEN)); |
| 302 |
sb.append(", B:"); |
| 303 |
sb.append(curveToString(CHANNEL_BLUE)); |
| 304 |
sb.append("}"); |
| 305 |
return sb.toString(); |
| 306 |
} |
| 307 |
|
| 308 |
private String curveToString(int colorChannel) { |
| 309 |
checkArgumentColorChannel(colorChannel); |
| 310 |
StringBuilder sb = new StringBuilder("["); |
| 311 |
float[] curve = getCurve(colorChannel); |
| 312 |
int pointCount = curve.length / POINT_SIZE; |
| 313 |
for (int i = 0, j = 0; i < pointCount; i++, j += 2) { |
| 314 |
sb.append("("); |
| 315 |
sb.append(curve[j]); |
| 316 |
sb.append(", "); |
| 317 |
sb.append(curve[j+1]); |
| 318 |
sb.append("), "); |
| 319 |
} |
| 320 |
|
| 321 |
sb.setLength(sb.length() - 2); |
| 322 |
sb.append("]"); |
| 323 |
return sb.toString(); |
| 324 |
} |
| 325 |
|
| 326 |
private float[] getCurve(int colorChannel) { |
| 327 |
switch (colorChannel) { |
| 328 |
case CHANNEL_RED: |
| 329 |
return mRed; |
| 330 |
case CHANNEL_GREEN: |
| 331 |
return mGreen; |
| 332 |
case CHANNEL_BLUE: |
| 333 |
return mBlue; |
| 334 |
default: |
| 335 |
throw new AssertionError("colorChannel out of range"); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
private final static int OFFSET_POINT_IN = 0; |
| 340 |
private final static int OFFSET_POINT_OUT = 1; |
| 341 |
private final static int TONEMAP_MIN_CURVE_POINTS = 2; |
| 342 |
private final static int MIN_CURVE_LENGTH = TONEMAP_MIN_CURVE_POINTS * POINT_SIZE; |
| 343 |
|
| 344 |
private final float[] mRed; |
| 345 |
private final float[] mGreen; |
| 346 |
private final float[] mBlue; |
| 347 |
|
| 348 |
private int mHashCode; |
| 349 |
private boolean mHashCalculated = false; |
| 350 |
} |