1/*
2 * Copyright (C) 2014 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 */
16package android.hardware.camera2.marshal.impl;
17
18import android.hardware.camera2.marshal.Marshaler;
19import android.hardware.camera2.marshal.MarshalQueryable;
20import android.hardware.camera2.params.ColorSpaceTransform;
21import android.hardware.camera2.utils.TypeReference;
22
23import java.nio.ByteBuffer;
24
25import static android.hardware.camera2.impl.CameraMetadataNative.*;
26import static android.hardware.camera2.marshal.MarshalHelpers.*;
27
28/**
29 * Marshal {@link ColorSpaceTransform} to/from {@link #TYPE_RATIONAL}
30 */
31public class MarshalQueryableColorSpaceTransform implements
32        MarshalQueryable<ColorSpaceTransform> {
33
34    private static final int ELEMENTS_INT32 = 3 * 3 * (SIZEOF_RATIONAL / SIZEOF_INT32);
35    private static final int SIZE = SIZEOF_INT32 * ELEMENTS_INT32;
36
37    /** rational x 3 x 3 */
38    private class MarshalerColorSpaceTransform extends Marshaler<ColorSpaceTransform> {
39        protected MarshalerColorSpaceTransform(TypeReference<ColorSpaceTransform> typeReference,
40                int nativeType) {
41            super(MarshalQueryableColorSpaceTransform.this, typeReference, nativeType);
42        }
43
44        @Override
45        public void marshal(ColorSpaceTransform value, ByteBuffer buffer) {
46            int[] transformAsArray = new int[ELEMENTS_INT32];
47            value.copyElements(transformAsArray, /*offset*/0);
48
49            for (int i = 0; i < ELEMENTS_INT32; ++i) {
50                buffer.putInt(transformAsArray[i]);
51            }
52        }
53
54        @Override
55        public ColorSpaceTransform unmarshal(ByteBuffer buffer) {
56            int[] transformAsArray = new int[ELEMENTS_INT32];
57
58            for (int i = 0; i < ELEMENTS_INT32; ++i) {
59                transformAsArray[i] = buffer.getInt();
60            }
61
62            return new ColorSpaceTransform(transformAsArray);
63        }
64
65        @Override
66        public int getNativeSize() {
67            return SIZE;
68        }
69    }
70
71    @Override
72    public Marshaler<ColorSpaceTransform> createMarshaler(
73            TypeReference<ColorSpaceTransform> managedType, int nativeType) {
74        return new MarshalerColorSpaceTransform(managedType, nativeType);
75    }
76
77    @Override
78    public boolean isTypeMappingSupported(
79            TypeReference<ColorSpaceTransform> managedType, int nativeType) {
80        return nativeType == TYPE_RATIONAL &&
81                ColorSpaceTransform.class.equals(managedType.getType());
82    }
83
84}
85