001package org.opencv.core;
002
003public final class CvType {
004
005    // type depth constants
006    public static final int
007            CV_8U = 0,
008            CV_8S = 1,
009            CV_16U = 2,
010            CV_16S = 3,
011            CV_32S = 4,
012            CV_32F = 5,
013            CV_64F = 6,
014            CV_16F = 7;
015
016    /**
017     * @deprecated please use {@link #CV_16F}
018     */
019    @Deprecated
020    public static final int CV_USRTYPE1 = CV_16F;
021
022    // predefined type constants
023    public static final int
024            CV_8UC1 = CV_8UC(1), CV_8UC2 = CV_8UC(2), CV_8UC3 = CV_8UC(3), CV_8UC4 = CV_8UC(4),
025            CV_8SC1 = CV_8SC(1), CV_8SC2 = CV_8SC(2), CV_8SC3 = CV_8SC(3), CV_8SC4 = CV_8SC(4),
026            CV_16UC1 = CV_16UC(1), CV_16UC2 = CV_16UC(2), CV_16UC3 = CV_16UC(3), CV_16UC4 = CV_16UC(4),
027            CV_16SC1 = CV_16SC(1), CV_16SC2 = CV_16SC(2), CV_16SC3 = CV_16SC(3), CV_16SC4 = CV_16SC(4),
028            CV_32SC1 = CV_32SC(1), CV_32SC2 = CV_32SC(2), CV_32SC3 = CV_32SC(3), CV_32SC4 = CV_32SC(4),
029            CV_32FC1 = CV_32FC(1), CV_32FC2 = CV_32FC(2), CV_32FC3 = CV_32FC(3), CV_32FC4 = CV_32FC(4),
030            CV_64FC1 = CV_64FC(1), CV_64FC2 = CV_64FC(2), CV_64FC3 = CV_64FC(3), CV_64FC4 = CV_64FC(4),
031            CV_16FC1 = CV_16FC(1), CV_16FC2 = CV_16FC(2), CV_16FC3 = CV_16FC(3), CV_16FC4 = CV_16FC(4);
032
033    private static final int CV_CN_MAX = 512, CV_CN_SHIFT = 3, CV_DEPTH_MAX = (1 << CV_CN_SHIFT);
034
035    public static final int makeType(int depth, int channels) {
036        if (channels <= 0 || channels >= CV_CN_MAX) {
037            throw new UnsupportedOperationException(
038                    "Channels count should be 1.." + (CV_CN_MAX - 1));
039        }
040        if (depth < 0 || depth >= CV_DEPTH_MAX) {
041            throw new UnsupportedOperationException(
042                    "Data type depth should be 0.." + (CV_DEPTH_MAX - 1));
043        }
044        return (depth & (CV_DEPTH_MAX - 1)) + ((channels - 1) << CV_CN_SHIFT);
045    }
046
047    public static final int CV_8UC(int ch) {
048        return makeType(CV_8U, ch);
049    }
050
051    public static final int CV_8SC(int ch) {
052        return makeType(CV_8S, ch);
053    }
054
055    public static final int CV_16UC(int ch) {
056        return makeType(CV_16U, ch);
057    }
058
059    public static final int CV_16SC(int ch) {
060        return makeType(CV_16S, ch);
061    }
062
063    public static final int CV_32SC(int ch) {
064        return makeType(CV_32S, ch);
065    }
066
067    public static final int CV_32FC(int ch) {
068        return makeType(CV_32F, ch);
069    }
070
071    public static final int CV_64FC(int ch) {
072        return makeType(CV_64F, ch);
073    }
074
075    public static final int CV_16FC(int ch) {
076        return makeType(CV_16F, ch);
077    }
078
079    public static final int channels(int type) {
080        return (type >> CV_CN_SHIFT) + 1;
081    }
082
083    public static final int depth(int type) {
084        return type & (CV_DEPTH_MAX - 1);
085    }
086
087    public static final boolean isInteger(int type) {
088        return depth(type) < CV_32F;
089    }
090
091    public static final int ELEM_SIZE(int type) {
092        switch (depth(type)) {
093        case CV_8U:
094        case CV_8S:
095            return channels(type);
096        case CV_16U:
097        case CV_16S:
098        case CV_16F:
099            return 2 * channels(type);
100        case CV_32S:
101        case CV_32F:
102            return 4 * channels(type);
103        case CV_64F:
104            return 8 * channels(type);
105        default:
106            throw new UnsupportedOperationException(
107                    "Unsupported CvType value: " + type);
108        }
109    }
110
111    public static final String typeToString(int type) {
112        String s;
113        switch (depth(type)) {
114        case CV_8U:
115            s = "CV_8U";
116            break;
117        case CV_8S:
118            s = "CV_8S";
119            break;
120        case CV_16U:
121            s = "CV_16U";
122            break;
123        case CV_16S:
124            s = "CV_16S";
125            break;
126        case CV_32S:
127            s = "CV_32S";
128            break;
129        case CV_32F:
130            s = "CV_32F";
131            break;
132        case CV_64F:
133            s = "CV_64F";
134            break;
135        case CV_16F:
136            s = "CV_16F";
137            break;
138        default:
139            throw new UnsupportedOperationException(
140                    "Unsupported CvType value: " + type);
141        }
142
143        int ch = channels(type);
144        if (ch <= 4)
145            return s + "C" + ch;
146        else
147            return s + "C(" + ch + ")";
148    }
149
150}