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