Add minimal support for FNC1 mode in QR Code
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / ECI.java
1 /*
2  * Copyright 2008 ZXing authors
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 com.google.zxing.qrcode.decoder;
18
19 import com.google.zxing.common.BitSource;
20
21 /**
22  * Superclass of classes encapsulating types ECIs, according to "Extended Channel Interpretations" 5.3.
23  *
24  * @author srowen@google.com (Sean Owen)
25  */
26 abstract class ECI {
27
28   private final int value;
29
30   ECI(int value) {
31     this.value = value;
32   }
33
34   int getValue() {
35     return value;
36   }
37
38   static ECI getECIByValue(int value) {
39     if (value < 0 || value > 999999) {
40       throw new IllegalArgumentException("Bad ECI value: " + value);
41     }
42     if (value < 900) { // Character set ECIs use 000000 - 000899
43       return CharacterSetECI.getCharacterSetECIByValue(value);
44     }
45     throw new IllegalArgumentException("Unsupported ECI value: " + value);
46   }
47
48   static int parseECI(BitSource bits) {
49     int firstByte = bits.readBits(8);
50     if ((firstByte & 0x80) == 0) {
51       // just one byte
52       return firstByte & 0x7F;
53     } else if ((firstByte & 0xC0) == 0x80) {
54       // two bytes
55       int secondByte = bits.readBits(8);
56       return ((firstByte & 0x3F) << 8) | secondByte;
57     } else if ((firstByte & 0xE0) == 0xC0) {
58       // three bytes
59       int secondThirdBytes = bits.readBits(16);
60       return ((firstByte & 0x1F) << 16) | secondThirdBytes;
61     }
62     throw new IllegalArgumentException("Bad ECI bits starting with byte " + firstByte);
63   }
64
65 }