1 /*
2  * Copyright (C) 2020 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 android.graphics;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 /**
25  * A {@link Parcelable} wrapper for a {@link ColorSpace}. In order to enable parceling, the
26  * ColorSpace must be either a {@link ColorSpace.Named Named} ColorSpace or a
27  * {@link ColorSpace.Rgb} instance that has an ICC parametric transfer function as returned by
28  * {@link ColorSpace.Rgb#getTransferParameters()}.
29  */
30 public final class ParcelableColorSpace implements Parcelable {
31     private final ColorSpace mColorSpace;
32 
33     /**
34      * Checks if the given ColorSpace is able to be parceled. A ColorSpace can only be
35      * parceled if it is a {@link ColorSpace.Named Named} ColorSpace or a {@link ColorSpace.Rgb}
36      * instance that has an ICC parametric transfer function as returned by
37      * {@link ColorSpace.Rgb#getTransferParameters()}
38      */
isParcelable(@onNull ColorSpace colorSpace)39     public static boolean isParcelable(@NonNull ColorSpace colorSpace) {
40         if (colorSpace.getId() == ColorSpace.MIN_ID) {
41             if (!(colorSpace instanceof ColorSpace.Rgb)) {
42                 return false;
43             }
44             ColorSpace.Rgb rgb = (ColorSpace.Rgb) colorSpace;
45             if (rgb.getTransferParameters() == null) {
46                 return false;
47             }
48         }
49         return true;
50     }
51 
52     /**
53      * Constructs a new ParcelableColorSpace that wraps the provided ColorSpace.
54      *
55      * @param colorSpace The ColorSpace to wrap. The ColorSpace must be either named or be an
56      *                   RGB ColorSpace with an ICC parametric transfer function.
57      * @throws IllegalArgumentException If the provided ColorSpace does not satisfy the requirements
58      * to be parceled. See {@link #isParcelable(ColorSpace)}.
59      */
ParcelableColorSpace(@onNull ColorSpace colorSpace)60     public ParcelableColorSpace(@NonNull ColorSpace colorSpace) {
61         mColorSpace = colorSpace;
62 
63         if (mColorSpace.getId() == ColorSpace.MIN_ID) {
64             if (!(mColorSpace instanceof ColorSpace.Rgb)) {
65                 throw new IllegalArgumentException(
66                         "Unable to parcel unknown ColorSpaces that are not ColorSpace.Rgb");
67             }
68             ColorSpace.Rgb rgb = (ColorSpace.Rgb) mColorSpace;
69             if (rgb.getTransferParameters() == null) {
70                 throw new IllegalArgumentException("ColorSpace must use an ICC "
71                         + "parametric transfer function to be parcelable");
72             }
73         }
74     }
75 
76     /**
77      * @return the backing ColorSpace that this ParcelableColorSpace is wrapping.
78      */
getColorSpace()79     public @NonNull ColorSpace getColorSpace() {
80         return mColorSpace;
81     }
82 
83     @Override
describeContents()84     public int describeContents() {
85         return 0;
86     }
87 
88     @Override
writeToParcel(@onNull Parcel dest, int flags)89     public void writeToParcel(@NonNull Parcel dest, int flags) {
90         final int id = mColorSpace.getId();
91         dest.writeInt(id);
92         if (id == ColorSpace.MIN_ID) {
93             // Not a named color space. We have to actually write, like, stuff. And things. Ugh.
94             // Cast is safe because this was asserted in the constructor
95             ColorSpace.Rgb rgb = (ColorSpace.Rgb) mColorSpace;
96             dest.writeString(rgb.getName());
97             dest.writeFloatArray(rgb.getPrimaries());
98             dest.writeFloatArray(rgb.getWhitePoint());
99             ColorSpace.Rgb.TransferParameters transferParameters = rgb.getTransferParameters();
100             dest.writeDouble(transferParameters.a);
101             dest.writeDouble(transferParameters.b);
102             dest.writeDouble(transferParameters.c);
103             dest.writeDouble(transferParameters.d);
104             dest.writeDouble(transferParameters.e);
105             dest.writeDouble(transferParameters.f);
106             dest.writeDouble(transferParameters.g);
107         }
108     }
109 
110     @NonNull
111     public static final Parcelable.Creator<ParcelableColorSpace> CREATOR =
112             new Parcelable.Creator<ParcelableColorSpace>() {
113 
114         public @NonNull ParcelableColorSpace createFromParcel(@NonNull Parcel in) {
115             final int id = in.readInt();
116             if (id == ColorSpace.MIN_ID) {
117                 String name = in.readString();
118                 float[] primaries = in.createFloatArray();
119                 float[] whitePoint = in.createFloatArray();
120                 double a = in.readDouble();
121                 double b = in.readDouble();
122                 double c = in.readDouble();
123                 double d = in.readDouble();
124                 double e = in.readDouble();
125                 double f = in.readDouble();
126                 double g = in.readDouble();
127                 ColorSpace.Rgb.TransferParameters function =
128                         new ColorSpace.Rgb.TransferParameters(a, b, c, d, e, f, g);
129                 return new ParcelableColorSpace(
130                         new ColorSpace.Rgb(name, primaries, whitePoint, function));
131             } else {
132                 return new ParcelableColorSpace(ColorSpace.get(id));
133             }
134         }
135 
136         public ParcelableColorSpace[] newArray(int size) {
137             return new ParcelableColorSpace[size];
138         }
139     };
140 
141     @Override
equals(@ullable Object o)142     public boolean equals(@Nullable Object o) {
143         if (this == o) return true;
144         if (o == null || getClass() != o.getClass()) return false;
145         ParcelableColorSpace other = (ParcelableColorSpace) o;
146         return mColorSpace.equals(other.mColorSpace);
147     }
148 
149     @Override
hashCode()150     public int hashCode() {
151         return mColorSpace.hashCode();
152     }
153 }
154