1 /*
2  * Copyright (C) 2011 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 
18 package android.filterfw.format;
19 
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.filterfw.core.FrameFormat;
22 import android.filterfw.core.MutableFrameFormat;
23 import android.graphics.Bitmap;
24 
25 /**
26  * @hide
27  */
28 public class ImageFormat {
29 
30     public static final String COLORSPACE_KEY = "colorspace";
31 
32     public static final int COLORSPACE_GRAY  = 1;
33     public static final int COLORSPACE_RGB   = 2;
34     public static final int COLORSPACE_RGBA  = 3;
35     public static final int COLORSPACE_YUV   = 4;
36 
create(int width, int height, int colorspace, int bytesPerSample, int target)37     public static MutableFrameFormat create(int width,
38                                             int height,
39                                             int colorspace,
40                                             int bytesPerSample,
41                                             int target) {
42         MutableFrameFormat result = new MutableFrameFormat(FrameFormat.TYPE_BYTE, target);
43         result.setDimensions(width, height);
44         result.setBytesPerSample(bytesPerSample);
45         result.setMetaValue(COLORSPACE_KEY, colorspace);
46         if (target == FrameFormat.TARGET_SIMPLE) {
47             result.setObjectClass(Bitmap.class);
48         }
49         return result;
50     }
51 
52     @UnsupportedAppUsage
create(int width, int height, int colorspace, int target)53     public static MutableFrameFormat create(int width,
54                                             int height,
55                                             int colorspace,
56                                             int target) {
57         return create(width,
58                       height,
59                       colorspace,
60                       bytesPerSampleForColorspace(colorspace),
61                       target);
62     }
63 
64     @UnsupportedAppUsage
create(int colorspace, int target)65     public static MutableFrameFormat create(int colorspace, int target) {
66         return create(FrameFormat.SIZE_UNSPECIFIED,
67                       FrameFormat.SIZE_UNSPECIFIED,
68                       colorspace,
69                       bytesPerSampleForColorspace(colorspace),
70                       target);
71     }
72 
73     @UnsupportedAppUsage
create(int colorspace)74     public static MutableFrameFormat create(int colorspace) {
75         return create(FrameFormat.SIZE_UNSPECIFIED,
76                       FrameFormat.SIZE_UNSPECIFIED,
77                       colorspace,
78                       bytesPerSampleForColorspace(colorspace),
79                       FrameFormat.TARGET_UNSPECIFIED);
80     }
81 
bytesPerSampleForColorspace(int colorspace)82     public static int bytesPerSampleForColorspace(int colorspace) {
83         switch (colorspace) {
84             case COLORSPACE_GRAY:
85                 return 1;
86             case COLORSPACE_RGB:
87                 return 3;
88             case COLORSPACE_RGBA:
89                 return 4;
90             case COLORSPACE_YUV:
91                 return 3;
92             default:
93                 throw new RuntimeException("Unknown colorspace id " + colorspace + "!");
94         }
95     }
96 }
97