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