├── 8x8.bmp ├── LICENSE ├── README.md ├── huffmanEncode.py ├── jpegEncoder.py └── lena.bmp /8x8.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fangwei123456/python-jpeg-encoder/d92dcf7718543d9f09c0df43bb7df2e6ada76394/8x8.bmp -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 fangwei123456 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-jpeg-encoder 2 | 3 | A jpeg encoder written in python. It can encode a BMP image file to a JPEG file. A jpeg decoder written in python is not needed because the encoder will create a standardized JPEG file, which can be opened with any picture viewer. 4 | 5 | ### **Usage:** 6 | 7 | `python jpegEncoder.py inputBMPFileName outputJPEGFileName quality(from 1 to 100) debugMode(0 or 1)` 8 | 9 | Example: 10 | 11 | `python ./jpegEncoder.py ./lena.bmp ./output.jpg 95 0` 12 | 13 | ### **More information:** 14 | 15 | If `debugMode` is 1, you will get more details about encoding process. 16 | 17 | For example, run `jpegEncoder.py` by: 18 | 19 | `python ./jpegEncoder.py ./8x8.bmp ./output.jpg 95 1` 20 | 21 | And you will get: 22 | 23 | srcImageWidth = 8 srcImageHeight = 8 24 | srcImage info: 25 | 26 | added to: 8 8 27 | yImageMatrix: 28 | [[255 255 255 255 255 255 255 255] 29 | [255 255 255 255 255 255 255 255] 30 | [255 255 255 255 255 255 255 255] 31 | [255 255 255 255 255 255 255 255] 32 | [255 255 255 255 255 255 255 255] 33 | [255 255 255 255 255 255 255 255] 34 | [255 255 255 255 255 255 255 255] 35 | [255 255 255 255 255 255 255 255]] 36 | uImageMatrix: 37 | [[128 128 128 128 128 128 128 128] 38 | [128 128 128 128 128 128 128 128] 39 | [128 128 128 128 128 128 128 128] 40 | [128 128 128 128 128 128 128 128] 41 | [128 128 128 128 128 128 128 128] 42 | [128 128 128 128 128 128 128 128] 43 | [128 128 128 128 128 128 128 128] 44 | [128 128 128 128 128 128 128 128]] 45 | vImageMatrix: 46 | [[128 128 128 128 128 128 128 128] 47 | [128 128 128 128 128 128 128 128] 48 | [128 128 128 128 128 128 128 128] 49 | [128 128 128 128 128 128 128 128] 50 | [128 128 128 128 128 128 128 128] 51 | [128 128 128 128 128 128 128 128] 52 | [128 128 128 128 128 128 128 128] 53 | [128 128 128 128 128 128 128 128]] 54 | luminanceQuantTbl: 55 | [[ 2 1 1 2 2 4 5 6] 56 | [ 1 1 1 2 3 6 6 6] 57 | [ 1 1 2 2 4 6 7 6] 58 | [ 1 2 2 3 5 9 8 6] 59 | [ 2 2 4 6 7 11 10 8] 60 | [ 2 4 6 6 8 10 11 9] 61 | [ 5 6 8 9 10 12 12 10] 62 | [ 7 9 10 10 11 10 10 10]] 63 | chrominanceQuantTbl: 64 | [[ 2 2 2 5 10 10 10 10] 65 | [ 2 2 3 7 10 10 10 10] 66 | [ 2 3 6 10 10 10 10 10] 67 | [ 5 7 10 10 10 10 10 10] 68 | [10 10 10 10 10 10 10 10] 69 | [10 10 10 10 10 10 10 10] 70 | [10 10 10 10 10 10 10 10] 71 | [10 10 10 10 10 10 10 10]] 72 | blockSum = 1 73 | block (y,x): 0 0 -> 8 8 74 | yDctMatrix: 75 | [[1024. 0. 0. 0. 0. 0. 0. 0.] 76 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 77 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 78 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 79 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 80 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 81 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 82 | [ 0. 0. 0. 0. 0. 0. 0. 0.]] 83 | uDctMatrix: 84 | [[8. 0. 0. 0. 0. 0. 0. 0.] 85 | [0. 0. 0. 0. 0. 0. 0. 0.] 86 | [0. 0. 0. 0. 0. 0. 0. 0.] 87 | [0. 0. 0. 0. 0. 0. 0. 0.] 88 | [0. 0. 0. 0. 0. 0. 0. 0.] 89 | [0. 0. 0. 0. 0. 0. 0. 0.] 90 | [0. 0. 0. 0. 0. 0. 0. 0.] 91 | [0. 0. 0. 0. 0. 0. 0. 0.]] 92 | vDctMatrix: 93 | [[8. 0. 0. 0. 0. 0. 0. 0.] 94 | [0. 0. 0. 0. 0. 0. 0. 0.] 95 | [0. 0. 0. 0. 0. 0. 0. 0.] 96 | [0. 0. 0. 0. 0. 0. 0. 0.] 97 | [0. 0. 0. 0. 0. 0. 0. 0.] 98 | [0. 0. 0. 0. 0. 0. 0. 0.] 99 | [0. 0. 0. 0. 0. 0. 0. 0.] 100 | [0. 0. 0. 0. 0. 0. 0. 0.]] 101 | yQuantMatrix: 102 | [[512. 0. 0. 0. 0. 0. 0. 0.] 103 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 104 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 105 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 106 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 107 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 108 | [ 0. 0. 0. 0. 0. 0. 0. 0.] 109 | [ 0. 0. 0. 0. 0. 0. 0. 0.]] 110 | uQuantMatrix: 111 | [[4. 0. 0. 0. 0. 0. 0. 0.] 112 | [0. 0. 0. 0. 0. 0. 0. 0.] 113 | [0. 0. 0. 0. 0. 0. 0. 0.] 114 | [0. 0. 0. 0. 0. 0. 0. 0.] 115 | [0. 0. 0. 0. 0. 0. 0. 0.] 116 | [0. 0. 0. 0. 0. 0. 0. 0.] 117 | [0. 0. 0. 0. 0. 0. 0. 0.] 118 | [0. 0. 0. 0. 0. 0. 0. 0.]] 119 | vQuantMatrix: 120 | [[4. 0. 0. 0. 0. 0. 0. 0.] 121 | [0. 0. 0. 0. 0. 0. 0. 0.] 122 | [0. 0. 0. 0. 0. 0. 0. 0.] 123 | [0. 0. 0. 0. 0. 0. 0. 0.] 124 | [0. 0. 0. 0. 0. 0. 0. 0.] 125 | [0. 0. 0. 0. 0. 0. 0. 0.] 126 | [0. 0. 0. 0. 0. 0. 0. 0.] 127 | [0. 0. 0. 0. 0. 0. 0. 0.]] 128 | encode dyDC: 512 129 | isLuminance= 1 (size,value)= 10 512 code= [1, 1, 1, 1, 1, 1, 0] [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] 130 | encode yAC: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 131 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 132 | EOB [1, 1, 0, 0] 133 | encode duDC: 4 134 | isLuminance= 0 (size,value)= 3 4 code= [1, 0, 1] [1, 0, 0] 135 | encode uAC: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 136 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 137 | EOB [0, 1] 138 | encode dvDC: 4 139 | isLuminance= 0 (size,value)= 3 4 code= [1, 0, 1] [1, 0, 0] 140 | encode vAC: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 141 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 142 | EOB [0, 1] 143 | 144 | 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /huffmanEncode.py: -------------------------------------------------------------------------------- 1 | from bitstream import BitStream 2 | import numpy 3 | from numpy import * 4 | from collections import OrderedDict 5 | 6 | #hexToBytes('F0') = 1111 1111 0000 0000(bytes) 7 | def hexToBytes(hexStr): 8 | num = len(hexStr)//2 9 | ret = numpy.zeros([num],dtype=int) 10 | for i in range(num): 11 | ret[i] = int(hexStr[2*i:2*i+2],16) 12 | 13 | ret = ret.tolist() 14 | ret = bytes(ret) 15 | return ret 16 | 17 | #Image Compression: JPEG Multimedia Systems (Module 4 Lesson 1).pdf 18 | 19 | #The DC Hoffman coding table for luminance recommended by JPEG 20 | DCLuminanceSizeToCode = [ 21 | [1,1,0], #0 EOB 22 | [1,0,1], #1 23 | [0,1,1], #2 24 | [0,1,0], #3 25 | [0,0,0], #4 26 | [0,0,1], #5 27 | [1,0,0], #6 28 | [1,1,1,0], #7 29 | [1,1,1,1,0], #8 30 | [1,1,1,1,1,0], #9 31 | [1,1,1,1,1,1,0], #10 0A 32 | [1,1,1,1,1,1,1,0] #11 0B 33 | ] 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | #The DC Hoffman coding table for chrominance recommended by JPEG 42 | DCChrominanceSizeToCode = [ 43 | [0,1], #0 EOB 44 | [0,0], #1 45 | [1,0,0], #2 46 | [1,0,1], #3 47 | [1,1,0,0], #4 48 | [1,1,0,1], #5 49 | [1,1,1,0], #6 50 | [1,1,1,1,0], #7 51 | [1,1,1,1,1,0], #8 52 | [1,1,1,1,1,1,0], #9 53 | [1,1,1,1,1,1,1,0], #10 0A 54 | [1,1,1,1,1,1,1,1,0]#11 0B 55 | ] 56 | 57 | 58 | ACLuminanceSizeToCode = { 59 | '01':[0,0], 60 | 61 | 62 | '02':[0,1], 63 | 64 | 65 | '03':[1,0,0], 66 | 67 | 68 | '11':[1,0,1,0], 69 | 70 | 71 | '04':[1,0,1,1], 72 | 73 | 74 | '00':[1,1,0,0], 75 | 76 | 77 | '05':[1,1,0,1,0], 78 | 79 | 80 | '21':[1,1,0,1,1], 81 | 82 | 83 | '12':[1,1,1,0,0], 84 | 85 | 86 | '31':[1,1,1,0,1,0], 87 | 88 | 89 | '41':[1,1,1,0,1,1], 90 | 91 | 92 | '51':[1,1,1,1,0,0,0], 93 | 94 | 95 | '06':[1,1,1,1,0,0,1], 96 | 97 | 98 | '13':[1,1,1,1,0,1,0], 99 | 100 | 101 | '61':[1,1,1,1,0,1,1], 102 | 103 | 104 | '22':[1,1,1,1,1,0,0,0], 105 | 106 | 107 | '71':[1,1,1,1,1,0,0,1], 108 | 109 | 110 | '81':[1,1,1,1,1,0,1,0,0], 111 | 112 | 113 | '14':[1,1,1,1,1,0,1,0,1], 114 | 115 | 116 | '32':[1,1,1,1,1,0,1,1,0], 117 | 118 | 119 | '91':[1,1,1,1,1,0,1,1,1], 120 | 121 | 122 | 'A1':[1,1,1,1,1,1,0,0,0], 123 | 124 | 125 | '07':[1,1,1,1,1,1,0,0,1], 126 | 127 | 128 | '15':[1,1,1,1,1,1,0,1,0,0], 129 | 130 | 131 | 'B1':[1,1,1,1,1,1,0,1,0,1], 132 | 133 | 134 | '42':[1,1,1,1,1,1,0,1,1,0], 135 | 136 | 137 | '23':[1,1,1,1,1,1,0,1,1,1], 138 | 139 | 140 | 'C1':[1,1,1,1,1,1,1,0,0,0], 141 | 142 | 143 | '52':[1,1,1,1,1,1,1,0,0,1], 144 | 145 | 146 | 'D1':[1,1,1,1,1,1,1,0,1,0], 147 | 148 | 149 | 'E1':[1,1,1,1,1,1,1,0,1,1,0], 150 | 151 | 152 | '33':[1,1,1,1,1,1,1,0,1,1,1], 153 | 154 | 155 | '16':[1,1,1,1,1,1,1,1,0,0,0], 156 | 157 | 158 | '62':[1,1,1,1,1,1,1,1,0,0,1,0], 159 | 160 | 161 | 'F0':[1,1,1,1,1,1,1,1,0,0,1,1], 162 | 163 | 164 | '24':[1,1,1,1,1,1,1,1,0,1,0,0], 165 | 166 | 167 | '72':[1,1,1,1,1,1,1,1,0,1,0,1], 168 | 169 | 170 | '82':[1,1,1,1,1,1,1,1,0,1,1,0,0], 171 | 172 | 173 | 'F1':[1,1,1,1,1,1,1,1,0,1,1,0,1], 174 | 175 | 176 | '25':[1,1,1,1,1,1,1,1,0,1,1,1,0,0], 177 | 178 | 179 | '43':[1,1,1,1,1,1,1,1,0,1,1,1,0,1], 180 | 181 | 182 | '34':[1,1,1,1,1,1,1,1,0,1,1,1,1,0], 183 | 184 | 185 | '53':[1,1,1,1,1,1,1,1,0,1,1,1,1,1], 186 | 187 | 188 | '92':[1,1,1,1,1,1,1,1,1,0,0,0,0,0], 189 | 190 | 191 | 'A2':[1,1,1,1,1,1,1,1,1,0,0,0,0,1], 192 | 193 | 194 | 'B2':[1,1,1,1,1,1,1,1,1,0,0,0,1,0,0], 195 | 196 | 197 | '63':[1,1,1,1,1,1,1,1,1,0,0,0,1,0,1], 198 | 199 | 200 | '73':[1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,0], 201 | 202 | 203 | 'C2':[1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,1], 204 | 205 | 206 | '35':[1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0], 207 | 208 | 209 | '44':[1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1], 210 | 211 | 212 | '27':[1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0], 213 | 214 | 215 | '93':[1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,1], 216 | 217 | 218 | 'A3':[1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,0], 219 | 220 | 221 | 'B3':[1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,1], 222 | 223 | 224 | '36':[1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,0], 225 | 226 | 227 | '17':[1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,1], 228 | 229 | 230 | '54':[1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,0], 231 | 232 | 233 | '64':[1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1], 234 | 235 | 236 | '74':[1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,0], 237 | 238 | 239 | 'C3':[1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,1], 240 | 241 | 242 | 'D2':[1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,0], 243 | 244 | 245 | 'E2':[1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,1], 246 | 247 | 248 | '08':[1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0], 249 | 250 | 251 | '26':[1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,1], 252 | 253 | 254 | '83':[1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,0], 255 | 256 | 257 | '09':[1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1], 258 | 259 | 260 | '0A':[1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0], 261 | 262 | 263 | '18':[1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1], 264 | 265 | 266 | '19':[1,1,1,1,1,1,1,1,1,0,1,0,0,0,1,0], 267 | 268 | 269 | '84':[1,1,1,1,1,1,1,1,1,0,1,0,0,0,1,1], 270 | 271 | 272 | '94':[1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,0], 273 | 274 | 275 | '45':[1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,1], 276 | 277 | 278 | '46':[1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,0], 279 | 280 | 281 | 'A4':[1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1], 282 | 283 | 284 | 'B4':[1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,0], 285 | 286 | 287 | '56':[1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,1], 288 | 289 | 290 | 'D3':[1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0], 291 | 292 | 293 | '55':[1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,1], 294 | 295 | 296 | '28':[1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,0], 297 | 298 | 299 | '1A':[1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,1], 300 | 301 | 302 | 'F2':[1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0], 303 | 304 | 305 | 'E3':[1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1], 306 | 307 | 308 | 'F3':[1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0], 309 | 310 | 311 | 'C4':[1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,1], 312 | 313 | 314 | 'D4':[1,1,1,1,1,1,1,1,1,0,1,1,0,0,1,0], 315 | 316 | 317 | 'E4':[1,1,1,1,1,1,1,1,1,0,1,1,0,0,1,1], 318 | 319 | 320 | 'F4':[1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0], 321 | 322 | 323 | '65':[1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1], 324 | 325 | 326 | '75':[1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0], 327 | 328 | 329 | '85':[1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1], 330 | 331 | 332 | '95':[1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,0], 333 | 334 | 335 | 'A5':[1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,1], 336 | 337 | 338 | 'B5':[1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0], 339 | 340 | 341 | 'C5':[1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1], 342 | 343 | 344 | 'D5':[1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0], 345 | 346 | 347 | 'E5':[1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1], 348 | 349 | 350 | 'F5':[1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0], 351 | 352 | 353 | '66':[1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1], 354 | 355 | 356 | '76':[1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0], 357 | 358 | 359 | '86':[1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1], 360 | 361 | 362 | '96':[1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0], 363 | 364 | 365 | 'A6':[1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1], 366 | 367 | 368 | 'B6':[1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,0], 369 | 370 | 371 | 'C6':[1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,1], 372 | 373 | 374 | 'D6':[1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0], 375 | 376 | 377 | 'E6':[1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1], 378 | 379 | 380 | 'F6':[1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0], 381 | 382 | 383 | '37':[1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1], 384 | 385 | 386 | '47':[1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,0], 387 | 388 | 389 | '57':[1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1], 390 | 391 | 392 | '67':[1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0], 393 | 394 | 395 | '77':[1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,1], 396 | 397 | 398 | '87':[1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0], 399 | 400 | 401 | '97':[1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1], 402 | 403 | 404 | 'A7':[1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0], 405 | 406 | 407 | 'B7':[1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,1], 408 | 409 | 410 | 'C7':[1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,0], 411 | 412 | 413 | 'D7':[1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,1], 414 | 415 | 416 | 'E7':[1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,0], 417 | 418 | 419 | 'F7':[1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1], 420 | 421 | 422 | '38':[1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,0], 423 | 424 | 425 | '48':[1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1], 426 | 427 | 428 | '58':[1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0], 429 | 430 | 431 | '68':[1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,1], 432 | 433 | 434 | '78':[1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0], 435 | 436 | 437 | '88':[1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1], 438 | 439 | 440 | '98':[1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0], 441 | 442 | 443 | 'A8':[1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1], 444 | 445 | 446 | 'B8':[1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0], 447 | 448 | 449 | 'C8':[1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1], 450 | 451 | 452 | 'D8':[1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0], 453 | 454 | 455 | 'E8':[1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1], 456 | 457 | 458 | 'F8':[1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0], 459 | 460 | 461 | '29':[1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1], 462 | 463 | 464 | '39':[1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0], 465 | 466 | 467 | '49':[1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1], 468 | 469 | 470 | '59':[1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0], 471 | 472 | 473 | '69':[1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1], 474 | 475 | 476 | '79':[1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0], 477 | 478 | 479 | '89':[1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1], 480 | 481 | 482 | '99':[1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0], 483 | 484 | 485 | 'A9':[1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1], 486 | 487 | 488 | 'B9':[1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,0], 489 | 490 | 491 | 'C9':[1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1], 492 | 493 | 494 | 'D9':[1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0], 495 | 496 | 497 | 'E9':[1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 498 | 499 | 500 | 'F9':[1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0], 501 | 502 | 503 | '2A':[1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1], 504 | 505 | 506 | '3A':[1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0], 507 | 508 | 509 | '4A':[1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1], 510 | 511 | 512 | '5A':[1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0], 513 | 514 | 515 | '6A':[1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1], 516 | 517 | 518 | '7A':[1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0], 519 | 520 | 521 | '8A':[1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1], 522 | 523 | 524 | '9A':[1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0], 525 | 526 | 527 | 'AA':[1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1], 528 | 529 | 530 | 'BA':[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0], 531 | 532 | 533 | 'CA':[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1], 534 | 535 | 536 | 'DA':[1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0], 537 | 538 | 539 | 'EA':[1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1], 540 | 541 | 542 | 'FA':[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0] 543 | } 544 | 545 | ACLuminanceSizeToCodeList = [ 546 | [0,0], 547 | 548 | [0,1], 549 | 550 | [1,0,0], 551 | 552 | [1,0,1,0], 553 | 554 | [1,0,1,1], 555 | 556 | [1,1,0,0], 557 | 558 | [1,1,0,1,0], 559 | 560 | [1,1,0,1,1], 561 | 562 | [1,1,1,0,0], 563 | 564 | [1,1,1,0,1,0], 565 | 566 | [1,1,1,0,1,1], 567 | 568 | [1,1,1,1,0,0,0], 569 | 570 | [1,1,1,1,0,0,1], 571 | 572 | [1,1,1,1,0,1,0], 573 | 574 | [1,1,1,1,0,1,1], 575 | 576 | [1,1,1,1,1,0,0,0], 577 | 578 | [1,1,1,1,1,0,0,1], 579 | 580 | [1,1,1,1,1,0,1,0,0], 581 | 582 | [1,1,1,1,1,0,1,0,1], 583 | 584 | [1,1,1,1,1,0,1,1,0], 585 | 586 | [1,1,1,1,1,0,1,1,1], 587 | 588 | [1,1,1,1,1,1,0,0,0], 589 | 590 | [1,1,1,1,1,1,0,0,1], 591 | 592 | [1,1,1,1,1,1,0,1,0,0], 593 | 594 | [1,1,1,1,1,1,0,1,0,1], 595 | 596 | [1,1,1,1,1,1,0,1,1,0], 597 | 598 | [1,1,1,1,1,1,0,1,1,1], 599 | 600 | [1,1,1,1,1,1,1,0,0,0], 601 | 602 | [1,1,1,1,1,1,1,0,0,1], 603 | 604 | [1,1,1,1,1,1,1,0,1,0], 605 | 606 | [1,1,1,1,1,1,1,0,1,1,0], 607 | 608 | [1,1,1,1,1,1,1,0,1,1,1], 609 | 610 | [1,1,1,1,1,1,1,1,0,0,0], 611 | 612 | [1,1,1,1,1,1,1,1,0,0,1,0], 613 | 614 | [1,1,1,1,1,1,1,1,0,0,1,1], 615 | 616 | [1,1,1,1,1,1,1,1,0,1,0,0], 617 | 618 | [1,1,1,1,1,1,1,1,0,1,0,1], 619 | 620 | [1,1,1,1,1,1,1,1,0,1,1,0,0], 621 | 622 | [1,1,1,1,1,1,1,1,0,1,1,0,1], 623 | 624 | [1,1,1,1,1,1,1,1,0,1,1,1,0,0], 625 | 626 | [1,1,1,1,1,1,1,1,0,1,1,1,0,1], 627 | 628 | [1,1,1,1,1,1,1,1,0,1,1,1,1,0], 629 | 630 | [1,1,1,1,1,1,1,1,0,1,1,1,1,1], 631 | 632 | [1,1,1,1,1,1,1,1,1,0,0,0,0,0], 633 | 634 | [1,1,1,1,1,1,1,1,1,0,0,0,0,1], 635 | 636 | [1,1,1,1,1,1,1,1,1,0,0,0,1,0,0], 637 | 638 | [1,1,1,1,1,1,1,1,1,0,0,0,1,0,1], 639 | 640 | [1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,0], 641 | 642 | [1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,1], 643 | 644 | [1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0], 645 | 646 | [1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1], 647 | 648 | [1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0], 649 | 650 | [1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,1], 651 | 652 | [1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,0], 653 | 654 | [1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,1], 655 | 656 | [1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,0], 657 | 658 | [1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,1], 659 | 660 | [1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,0], 661 | 662 | [1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1], 663 | 664 | [1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,0], 665 | 666 | [1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,1], 667 | 668 | [1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,0], 669 | 670 | [1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,1], 671 | 672 | [1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0], 673 | 674 | [1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,1], 675 | 676 | [1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,0], 677 | 678 | [1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1], 679 | 680 | [1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0], 681 | 682 | [1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1], 683 | 684 | [1,1,1,1,1,1,1,1,1,0,1,0,0,0,1,0], 685 | 686 | [1,1,1,1,1,1,1,1,1,0,1,0,0,0,1,1], 687 | 688 | [1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,0], 689 | 690 | [1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,1], 691 | 692 | [1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,0], 693 | 694 | [1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1], 695 | 696 | [1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,0], 697 | 698 | [1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,1], 699 | 700 | [1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0], 701 | 702 | [1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,1], 703 | 704 | [1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,0], 705 | 706 | [1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,1], 707 | 708 | [1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0], 709 | 710 | [1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1], 711 | 712 | [1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0], 713 | 714 | [1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,1], 715 | 716 | [1,1,1,1,1,1,1,1,1,0,1,1,0,0,1,0], 717 | 718 | [1,1,1,1,1,1,1,1,1,0,1,1,0,0,1,1], 719 | 720 | [1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0], 721 | 722 | [1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1], 723 | 724 | [1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0], 725 | 726 | [1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1], 727 | 728 | [1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,0], 729 | 730 | [1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,1], 731 | 732 | [1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0], 733 | 734 | [1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1], 735 | 736 | [1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0], 737 | 738 | [1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1], 739 | 740 | [1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0], 741 | 742 | [1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1], 743 | 744 | [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0], 745 | 746 | [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1], 747 | 748 | [1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0], 749 | 750 | [1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1], 751 | 752 | [1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,0], 753 | 754 | [1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,1], 755 | 756 | [1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0], 757 | 758 | [1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1], 759 | 760 | [1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0], 761 | 762 | [1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1], 763 | 764 | [1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,0], 765 | 766 | [1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1], 767 | 768 | [1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0], 769 | 770 | [1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,1], 771 | 772 | [1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0], 773 | 774 | [1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1], 775 | 776 | [1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0], 777 | 778 | [1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,1], 779 | 780 | [1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,0], 781 | 782 | [1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,1], 783 | 784 | [1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,0], 785 | 786 | [1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1], 787 | 788 | [1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,0], 789 | 790 | [1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1], 791 | 792 | [1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0], 793 | 794 | [1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,1], 795 | 796 | [1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0], 797 | 798 | [1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1], 799 | 800 | [1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0], 801 | 802 | [1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1], 803 | 804 | [1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0], 805 | 806 | [1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1], 807 | 808 | [1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0], 809 | 810 | [1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1], 811 | 812 | [1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0], 813 | 814 | [1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1], 815 | 816 | [1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0], 817 | 818 | [1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1], 819 | 820 | [1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0], 821 | 822 | [1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1], 823 | 824 | [1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0], 825 | 826 | [1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1], 827 | 828 | [1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0], 829 | 830 | [1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1], 831 | 832 | [1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,0], 833 | 834 | [1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1], 835 | 836 | [1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0], 837 | 838 | [1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 839 | 840 | [1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0], 841 | 842 | [1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1], 843 | 844 | [1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0], 845 | 846 | [1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1], 847 | 848 | [1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0], 849 | 850 | [1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1], 851 | 852 | [1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0], 853 | 854 | [1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1], 855 | 856 | [1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0], 857 | 858 | [1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1], 859 | 860 | [1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0], 861 | 862 | [1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1], 863 | 864 | [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0], 865 | 866 | [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1], 867 | 868 | [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0] 869 | ] 870 | 871 | 872 | 873 | ACChrominanceToCode = { 874 | '01':[0,0], 875 | 876 | 877 | '00':[0,1], 878 | 879 | 880 | '02':[1,0,0], 881 | 882 | 883 | '11':[1,0,1], 884 | 885 | 886 | '03':[1,1,0,0], 887 | 888 | 889 | '04':[1,1,0,1,0], 890 | 891 | 892 | '21':[1,1,0,1,1], 893 | 894 | 895 | '12':[1,1,1,0,0,0], 896 | 897 | 898 | '31':[1,1,1,0,0,1], 899 | 900 | 901 | '41':[1,1,1,0,1,0], 902 | 903 | 904 | '05':[1,1,1,0,1,1,0], 905 | 906 | 907 | '51':[1,1,1,0,1,1,1], 908 | 909 | 910 | '13':[1,1,1,1,0,0,0], 911 | 912 | 913 | '61':[1,1,1,1,0,0,1], 914 | 915 | 916 | '22':[1,1,1,1,0,1,0], 917 | 918 | 919 | '06':[1,1,1,1,0,1,1,0], 920 | 921 | 922 | '71':[1,1,1,1,0,1,1,1], 923 | 924 | 925 | '81':[1,1,1,1,1,0,0,0], 926 | 927 | 928 | '91':[1,1,1,1,1,0,0,1], 929 | 930 | 931 | '32':[1,1,1,1,1,0,1,0], 932 | 933 | 934 | 'A1':[1,1,1,1,1,0,1,1,0], 935 | 936 | 937 | 'B1':[1,1,1,1,1,0,1,1,1], 938 | 939 | 940 | 'F0':[1,1,1,1,1,1,0,0,0], 941 | 942 | 943 | '14':[1,1,1,1,1,1,0,0,1], 944 | 945 | 946 | 'C1':[1,1,1,1,1,1,0,1,0,0], 947 | 948 | 949 | 'D1':[1,1,1,1,1,1,0,1,0,1], 950 | 951 | 952 | 'E1':[1,1,1,1,1,1,0,1,1,0], 953 | 954 | 955 | '23':[1,1,1,1,1,1,0,1,1,1], 956 | 957 | 958 | '42':[1,1,1,1,1,1,1,0,0,0], 959 | 960 | 961 | '15':[1,1,1,1,1,1,1,0,0,1,0], 962 | 963 | 964 | '52':[1,1,1,1,1,1,1,0,0,1,1], 965 | 966 | 967 | '62':[1,1,1,1,1,1,1,0,1,0,0], 968 | 969 | 970 | '72':[1,1,1,1,1,1,1,0,1,0,1], 971 | 972 | 973 | 'F1':[1,1,1,1,1,1,1,0,1,1,0], 974 | 975 | 976 | '33':[1,1,1,1,1,1,1,0,1,1,1], 977 | 978 | 979 | '24':[1,1,1,1,1,1,1,1,0,0,0,0], 980 | 981 | 982 | '34':[1,1,1,1,1,1,1,1,0,0,0,1], 983 | 984 | 985 | '43':[1,1,1,1,1,1,1,1,0,0,1,0], 986 | 987 | 988 | '82':[1,1,1,1,1,1,1,1,0,0,1,1], 989 | 990 | 991 | '16':[1,1,1,1,1,1,1,1,0,1,0,0,0], 992 | 993 | 994 | '92':[1,1,1,1,1,1,1,1,0,1,0,0,1], 995 | 996 | 997 | '53':[1,1,1,1,1,1,1,1,0,1,0,1,0], 998 | 999 | 1000 | '25':[1,1,1,1,1,1,1,1,0,1,0,1,1], 1001 | 1002 | 1003 | 'A2':[1,1,1,1,1,1,1,1,0,1,1,0,0], 1004 | 1005 | 1006 | '63':[1,1,1,1,1,1,1,1,0,1,1,0,1], 1007 | 1008 | 1009 | 'B2':[1,1,1,1,1,1,1,1,0,1,1,1,0], 1010 | 1011 | 1012 | 'C2':[1,1,1,1,1,1,1,1,0,1,1,1,1], 1013 | 1014 | 1015 | '07':[1,1,1,1,1,1,1,1,1,0,0,0,0,0], 1016 | 1017 | 1018 | '73':[1,1,1,1,1,1,1,1,1,0,0,0,0,1], 1019 | 1020 | 1021 | 'D2':[1,1,1,1,1,1,1,1,1,0,0,0,1,0], 1022 | 1023 | 1024 | '35':[1,1,1,1,1,1,1,1,1,0,0,0,1,1,0], 1025 | 1026 | 1027 | 'E2':[1,1,1,1,1,1,1,1,1,0,0,0,1,1,1], 1028 | 1029 | 1030 | '44':[1,1,1,1,1,1,1,1,1,0,0,1,0,0,0], 1031 | 1032 | 1033 | '83':[1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,0], 1034 | 1035 | 1036 | '17':[1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,1], 1037 | 1038 | 1039 | '54':[1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,0], 1040 | 1041 | 1042 | '93':[1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,1], 1043 | 1044 | 1045 | '08':[1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,0], 1046 | 1047 | 1048 | '09':[1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1], 1049 | 1050 | 1051 | '0A':[1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,0], 1052 | 1053 | 1054 | '18':[1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,1], 1055 | 1056 | 1057 | '19':[1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,0], 1058 | 1059 | 1060 | '26':[1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,1], 1061 | 1062 | 1063 | '36':[1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0], 1064 | 1065 | 1066 | '45':[1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,1], 1067 | 1068 | 1069 | '1A':[1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,0], 1070 | 1071 | 1072 | '27':[1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1], 1073 | 1074 | 1075 | '64':[1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0], 1076 | 1077 | 1078 | '74':[1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1], 1079 | 1080 | 1081 | '55':[1,1,1,1,1,1,1,1,1,0,1,0,0,0,1,0], 1082 | 1083 | 1084 | '37':[1,1,1,1,1,1,1,1,1,0,1,0,0,0,1,1], 1085 | 1086 | 1087 | 'F2':[1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,0], 1088 | 1089 | 1090 | 'A3':[1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,1], 1091 | 1092 | 1093 | 'B3':[1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,0], 1094 | 1095 | 1096 | 'C3':[1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1], 1097 | 1098 | 1099 | '28':[1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,0], 1100 | 1101 | 1102 | '29':[1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,1], 1103 | 1104 | 1105 | 'D3':[1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0], 1106 | 1107 | 1108 | 'E3':[1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,1], 1109 | 1110 | 1111 | 'F3':[1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,0], 1112 | 1113 | 1114 | '84':[1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,1], 1115 | 1116 | 1117 | '94':[1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0], 1118 | 1119 | 1120 | 'A4':[1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1], 1121 | 1122 | 1123 | 'B4':[1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0], 1124 | 1125 | 1126 | 'C4':[1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,1], 1127 | 1128 | 1129 | 'D4':[1,1,1,1,1,1,1,1,1,0,1,1,0,0,1,0], 1130 | 1131 | 1132 | 'E4':[1,1,1,1,1,1,1,1,1,0,1,1,0,0,1,1], 1133 | 1134 | 1135 | 'F4':[1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0], 1136 | 1137 | 1138 | '65':[1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1], 1139 | 1140 | 1141 | '75':[1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0], 1142 | 1143 | 1144 | '85':[1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1], 1145 | 1146 | 1147 | '95':[1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,0], 1148 | 1149 | 1150 | 'A5':[1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,1], 1151 | 1152 | 1153 | 'B5':[1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0], 1154 | 1155 | 1156 | 'C5':[1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1], 1157 | 1158 | 1159 | 'D5':[1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0], 1160 | 1161 | 1162 | 'E5':[1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1], 1163 | 1164 | 1165 | 'F5':[1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0], 1166 | 1167 | 1168 | '46':[1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1], 1169 | 1170 | 1171 | '56':[1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0], 1172 | 1173 | 1174 | '66':[1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1], 1175 | 1176 | 1177 | '76':[1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0], 1178 | 1179 | 1180 | '86':[1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1], 1181 | 1182 | 1183 | '96':[1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,0], 1184 | 1185 | 1186 | 'A6':[1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,1], 1187 | 1188 | 1189 | 'B6':[1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0], 1190 | 1191 | 1192 | 'C6':[1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1], 1193 | 1194 | 1195 | 'D6':[1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0], 1196 | 1197 | 1198 | 'E6':[1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1], 1199 | 1200 | 1201 | 'F6':[1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,0], 1202 | 1203 | 1204 | '47':[1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1], 1205 | 1206 | 1207 | '57':[1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0], 1208 | 1209 | 1210 | '67':[1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,1], 1211 | 1212 | 1213 | '77':[1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0], 1214 | 1215 | 1216 | '87':[1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1], 1217 | 1218 | 1219 | '97':[1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0], 1220 | 1221 | 1222 | 'A7':[1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,1], 1223 | 1224 | 1225 | 'B7':[1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,0], 1226 | 1227 | 1228 | 'C7':[1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,1], 1229 | 1230 | 1231 | 'D7':[1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,0], 1232 | 1233 | 1234 | 'E7':[1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1], 1235 | 1236 | 1237 | 'F7':[1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,0], 1238 | 1239 | 1240 | '38':[1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1], 1241 | 1242 | 1243 | '48':[1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0], 1244 | 1245 | 1246 | '58':[1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,1], 1247 | 1248 | 1249 | '68':[1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0], 1250 | 1251 | 1252 | '78':[1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1], 1253 | 1254 | 1255 | '88':[1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0], 1256 | 1257 | 1258 | '98':[1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1], 1259 | 1260 | 1261 | 'A8':[1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0], 1262 | 1263 | 1264 | 'B8':[1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1], 1265 | 1266 | 1267 | 'C8':[1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0], 1268 | 1269 | 1270 | 'D8':[1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1], 1271 | 1272 | 1273 | 'E8':[1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0], 1274 | 1275 | 1276 | 'F8':[1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1], 1277 | 1278 | 1279 | '39':[1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0], 1280 | 1281 | 1282 | '49':[1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1], 1283 | 1284 | 1285 | '59':[1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0], 1286 | 1287 | 1288 | '69':[1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1], 1289 | 1290 | 1291 | '79':[1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0], 1292 | 1293 | 1294 | '89':[1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1], 1295 | 1296 | 1297 | '99':[1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0], 1298 | 1299 | 1300 | 'A9':[1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1], 1301 | 1302 | 1303 | 'B9':[1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,0], 1304 | 1305 | 1306 | 'C9':[1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1], 1307 | 1308 | 1309 | 'D9':[1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0], 1310 | 1311 | 1312 | 'E9':[1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 1313 | 1314 | 1315 | 'F9':[1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0], 1316 | 1317 | 1318 | '2A':[1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1], 1319 | 1320 | 1321 | '3A':[1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0], 1322 | 1323 | 1324 | '4A':[1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1], 1325 | 1326 | 1327 | '5A':[1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0], 1328 | 1329 | 1330 | '6A':[1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1], 1331 | 1332 | 1333 | '7A':[1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0], 1334 | 1335 | 1336 | '8A':[1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1], 1337 | 1338 | 1339 | '9A':[1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0], 1340 | 1341 | 1342 | 'AA':[1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1], 1343 | 1344 | 1345 | 'BA':[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0], 1346 | 1347 | 1348 | 'CA':[1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1], 1349 | 1350 | 1351 | 'DA':[1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0], 1352 | 1353 | 1354 | 'EA':[1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1], 1355 | 1356 | 1357 | 'FA':[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0], 1358 | } 1359 | 1360 | ACChrominanceToCodeList = [ 1361 | [0,0], 1362 | 1363 | [0,1], 1364 | 1365 | [1,0,0], 1366 | 1367 | [1,0,1], 1368 | 1369 | [1,1,0,0], 1370 | 1371 | [1,1,0,1,0], 1372 | 1373 | [1,1,0,1,1], 1374 | 1375 | [1,1,1,0,0,0], 1376 | 1377 | [1,1,1,0,0,1], 1378 | 1379 | [1,1,1,0,1,0], 1380 | 1381 | [1,1,1,0,1,1,0], 1382 | 1383 | [1,1,1,0,1,1,1], 1384 | 1385 | [1,1,1,1,0,0,0], 1386 | 1387 | [1,1,1,1,0,0,1], 1388 | 1389 | [1,1,1,1,0,1,0], 1390 | 1391 | [1,1,1,1,0,1,1,0], 1392 | 1393 | [1,1,1,1,0,1,1,1], 1394 | 1395 | [1,1,1,1,1,0,0,0], 1396 | 1397 | [1,1,1,1,1,0,0,1], 1398 | 1399 | [1,1,1,1,1,0,1,0], 1400 | 1401 | [1,1,1,1,1,0,1,1,0], 1402 | 1403 | [1,1,1,1,1,0,1,1,1], 1404 | 1405 | [1,1,1,1,1,1,0,0,0], 1406 | 1407 | [1,1,1,1,1,1,0,0,1], 1408 | 1409 | [1,1,1,1,1,1,0,1,0,0], 1410 | 1411 | [1,1,1,1,1,1,0,1,0,1], 1412 | 1413 | [1,1,1,1,1,1,0,1,1,0], 1414 | 1415 | [1,1,1,1,1,1,0,1,1,1], 1416 | 1417 | [1,1,1,1,1,1,1,0,0,0], 1418 | 1419 | [1,1,1,1,1,1,1,0,0,1,0], 1420 | 1421 | [1,1,1,1,1,1,1,0,0,1,1], 1422 | 1423 | [1,1,1,1,1,1,1,0,1,0,0], 1424 | 1425 | [1,1,1,1,1,1,1,0,1,0,1], 1426 | 1427 | [1,1,1,1,1,1,1,0,1,1,0], 1428 | 1429 | [1,1,1,1,1,1,1,0,1,1,1], 1430 | 1431 | [1,1,1,1,1,1,1,1,0,0,0,0], 1432 | 1433 | [1,1,1,1,1,1,1,1,0,0,0,1], 1434 | 1435 | [1,1,1,1,1,1,1,1,0,0,1,0], 1436 | 1437 | [1,1,1,1,1,1,1,1,0,0,1,1], 1438 | 1439 | [1,1,1,1,1,1,1,1,0,1,0,0,0], 1440 | 1441 | [1,1,1,1,1,1,1,1,0,1,0,0,1], 1442 | 1443 | [1,1,1,1,1,1,1,1,0,1,0,1,0], 1444 | 1445 | [1,1,1,1,1,1,1,1,0,1,0,1,1], 1446 | 1447 | [1,1,1,1,1,1,1,1,0,1,1,0,0], 1448 | 1449 | [1,1,1,1,1,1,1,1,0,1,1,0,1], 1450 | 1451 | [1,1,1,1,1,1,1,1,0,1,1,1,0], 1452 | 1453 | [1,1,1,1,1,1,1,1,0,1,1,1,1], 1454 | 1455 | [1,1,1,1,1,1,1,1,1,0,0,0,0,0], 1456 | 1457 | [1,1,1,1,1,1,1,1,1,0,0,0,0,1], 1458 | 1459 | [1,1,1,1,1,1,1,1,1,0,0,0,1,0], 1460 | 1461 | [1,1,1,1,1,1,1,1,1,0,0,0,1,1,0], 1462 | 1463 | [1,1,1,1,1,1,1,1,1,0,0,0,1,1,1], 1464 | 1465 | [1,1,1,1,1,1,1,1,1,0,0,1,0,0,0], 1466 | 1467 | [1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,0], 1468 | 1469 | [1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,1], 1470 | 1471 | [1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,0], 1472 | 1473 | [1,1,1,1,1,1,1,1,1,0,0,1,0,1,0,1], 1474 | 1475 | [1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,0], 1476 | 1477 | [1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1], 1478 | 1479 | [1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,0], 1480 | 1481 | [1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,1], 1482 | 1483 | [1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,0], 1484 | 1485 | [1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,1], 1486 | 1487 | [1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0], 1488 | 1489 | [1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,1], 1490 | 1491 | [1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,0], 1492 | 1493 | [1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1], 1494 | 1495 | [1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0], 1496 | 1497 | [1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1], 1498 | 1499 | [1,1,1,1,1,1,1,1,1,0,1,0,0,0,1,0], 1500 | 1501 | [1,1,1,1,1,1,1,1,1,0,1,0,0,0,1,1], 1502 | 1503 | [1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,0], 1504 | 1505 | [1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,1], 1506 | 1507 | [1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,0], 1508 | 1509 | [1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1], 1510 | 1511 | [1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,0], 1512 | 1513 | [1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,1], 1514 | 1515 | [1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0], 1516 | 1517 | [1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,1], 1518 | 1519 | [1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,0], 1520 | 1521 | [1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,1], 1522 | 1523 | [1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0], 1524 | 1525 | [1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1], 1526 | 1527 | [1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0], 1528 | 1529 | [1,1,1,1,1,1,1,1,1,0,1,1,0,0,0,1], 1530 | 1531 | [1,1,1,1,1,1,1,1,1,0,1,1,0,0,1,0], 1532 | 1533 | [1,1,1,1,1,1,1,1,1,0,1,1,0,0,1,1], 1534 | 1535 | [1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0], 1536 | 1537 | [1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1], 1538 | 1539 | [1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0], 1540 | 1541 | [1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1], 1542 | 1543 | [1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,0], 1544 | 1545 | [1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,1], 1546 | 1547 | [1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,0], 1548 | 1549 | [1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1], 1550 | 1551 | [1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0], 1552 | 1553 | [1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1], 1554 | 1555 | [1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0], 1556 | 1557 | [1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1], 1558 | 1559 | [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0], 1560 | 1561 | [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1], 1562 | 1563 | [1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0], 1564 | 1565 | [1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1], 1566 | 1567 | [1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,0], 1568 | 1569 | [1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,1], 1570 | 1571 | [1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0], 1572 | 1573 | [1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1], 1574 | 1575 | [1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0], 1576 | 1577 | [1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1], 1578 | 1579 | [1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,0], 1580 | 1581 | [1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1], 1582 | 1583 | [1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0], 1584 | 1585 | [1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,1], 1586 | 1587 | [1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0], 1588 | 1589 | [1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1], 1590 | 1591 | [1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0], 1592 | 1593 | [1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,1], 1594 | 1595 | [1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,0], 1596 | 1597 | [1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,1], 1598 | 1599 | [1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,0], 1600 | 1601 | [1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1], 1602 | 1603 | [1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,0], 1604 | 1605 | [1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1], 1606 | 1607 | [1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,0], 1608 | 1609 | [1,1,1,1,1,1,1,1,1,1,0,1,1,0,0,1], 1610 | 1611 | [1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0], 1612 | 1613 | [1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1], 1614 | 1615 | [1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0], 1616 | 1617 | [1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1], 1618 | 1619 | [1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0], 1620 | 1621 | [1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1], 1622 | 1623 | [1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0], 1624 | 1625 | [1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1], 1626 | 1627 | [1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0], 1628 | 1629 | [1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1], 1630 | 1631 | [1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0], 1632 | 1633 | [1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1], 1634 | 1635 | [1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0], 1636 | 1637 | [1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1], 1638 | 1639 | [1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0], 1640 | 1641 | [1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1], 1642 | 1643 | [1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0], 1644 | 1645 | [1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1], 1646 | 1647 | [1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,0], 1648 | 1649 | [1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1], 1650 | 1651 | [1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0], 1652 | 1653 | [1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 1654 | 1655 | [1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0], 1656 | 1657 | [1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1], 1658 | 1659 | [1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0], 1660 | 1661 | [1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1], 1662 | 1663 | [1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0], 1664 | 1665 | [1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1], 1666 | 1667 | [1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0], 1668 | 1669 | [1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1], 1670 | 1671 | [1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0], 1672 | 1673 | [1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1], 1674 | 1675 | [1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0], 1676 | 1677 | [1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1], 1678 | 1679 | [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0], 1680 | 1681 | [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1], 1682 | 1683 | [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0] 1684 | ] 1685 | 1686 | 1687 | 1688 | 1689 | #DC components are differentially coded as (SIZE,Value) 1690 | def encodeDCToBoolList(value,isLuminance,debugMode = 0): 1691 | boolList = [] 1692 | size = int(value).bit_length() # int(0).bit_length()=0 1693 | if(isLuminance==1): 1694 | boolList = boolList + DCLuminanceSizeToCode[size] 1695 | else: 1696 | boolList = boolList + DCChrominanceSizeToCode[size] 1697 | if(value<=0): # if value==0, codeList = [], (SIZE,VALUE)=(SIZE)=EOB 1698 | codeList = list(bin(value)[3:]) 1699 | for i in range(len(codeList)): 1700 | if (codeList[i] == '0'): 1701 | codeList[i] = 1 1702 | else: 1703 | codeList[i] = 0 1704 | else: 1705 | codeList = list(bin(value)[2:]) 1706 | for i in range(len(codeList)): 1707 | if (codeList[i] == '0'): 1708 | codeList[i] = 0 1709 | else: 1710 | codeList[i] = 1 1711 | boolList = boolList + codeList 1712 | if(debugMode == 1): 1713 | if(isLuminance==1): 1714 | print('isLuminance=',isLuminance,'(size,value)=',size,value,'code=',DCLuminanceSizeToCode[size],codeList) 1715 | else: 1716 | print('isLuminance=', isLuminance, '(size,value)=', size, value, 'code=', DCChrominanceSizeToCode[size],codeList) 1717 | return boolList 1718 | 1719 | def encodeACBlock(bitStream,ACArray,isLuminance,debugMode = 0): 1720 | 1721 | i = 0 1722 | maxI = numpy.size(ACArray) 1723 | while 1: 1724 | if(i==maxI): 1725 | break 1726 | run = 0 1727 | 1728 | # check if rest of ACArray are all zero. If so, just write EOB and return 1729 | j = i 1730 | while 1: 1731 | if(ACArray[j]!=0): 1732 | break 1733 | if(j==maxI - 1): 1734 | if (isLuminance == 1): 1735 | bitStream.write(ACLuminanceSizeToCode['00'], bool) # EOB 1736 | if (debugMode == 1): 1737 | print('EOB', ACLuminanceSizeToCode['00']) 1738 | else: 1739 | bitStream.write(ACChrominanceToCode['00'], bool) 1740 | if (debugMode == 1): 1741 | print('EOB', ACChrominanceToCode['00']) 1742 | return 1743 | j = j + 1 1744 | 1745 | 1746 | 1747 | while 1: 1748 | if(ACArray[i]!=0 or i==maxI - 1 or run==15): 1749 | break 1750 | else: 1751 | run = run + 1 1752 | i = i + 1 1753 | 1754 | value = ACArray[i] 1755 | 1756 | if(value==0 and run!=15): 1757 | break # Rest of the components are zeros therefore we simply put the EOB to signify this fact 1758 | 1759 | size = int(value).bit_length() 1760 | 1761 | runSizeStr = str.upper(str(hex(run))[2:]) + str.upper(str(hex(size))[2:]) 1762 | 1763 | 1764 | if (isLuminance == 1): 1765 | bitStream.write(ACLuminanceSizeToCode[runSizeStr], bool) 1766 | else: 1767 | bitStream.write(ACChrominanceToCode[runSizeStr], bool) 1768 | 1769 | 1770 | if(value<=0):# if value==0, codeList = [], (SIZE,VALUE)=(SIZE,[])=EOB 1771 | codeList = list(bin(value)[3:]) 1772 | for k in range(len(codeList)): 1773 | if (codeList[k] == '0'): 1774 | codeList[k] = 1 1775 | else: 1776 | codeList[k] = 0 1777 | else: 1778 | codeList = list(bin(value)[2:]) 1779 | for k in range(len(codeList)): 1780 | if (codeList[k] == '0'): 1781 | codeList[k] = 0 1782 | else: 1783 | codeList[k] = 1 1784 | bitStream.write(codeList, bool) 1785 | if(debugMode == 1): 1786 | if(isLuminance==1): 1787 | print('isLuminance=',isLuminance,'(run,size,value)=',run,size,value,'code=',ACLuminanceSizeToCode[runSizeStr],codeList) 1788 | else: 1789 | print('isLuminance=',isLuminance,'(run,size,value)=',run,size,value,'code=',ACChrominanceToCode[runSizeStr],codeList) 1790 | i = i + 1 1791 | 1792 | 1793 | 1794 | 1795 | 1796 | -------------------------------------------------------------------------------- /jpegEncoder.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | from scipy import fftpack 3 | import numpy 4 | from bitstream import BitStream 5 | from numpy import * 6 | import huffmanEncode 7 | import sys 8 | 9 | 10 | 11 | 12 | zigzagOrder = numpy.array([0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42, 13 | 49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63]) 14 | #std_quant_tbl from libjpeg::jcparam.c 15 | 16 | 17 | std_luminance_quant_tbl = numpy.array( 18 | [ 16, 11, 10, 16, 24, 40, 51, 61, 19 | 12, 12, 14, 19, 26, 58, 60, 55, 20 | 14, 13, 16, 24, 40, 57, 69, 56, 21 | 14, 17, 22, 29, 51, 87, 80, 62, 22 | 18, 22, 37, 56, 68, 109, 103, 77, 23 | 24, 35, 55, 64, 81, 104, 113, 92, 24 | 49, 64, 78, 87, 103, 121, 120, 101, 25 | 72, 92, 95, 98, 112, 100, 103, 99],dtype=int) 26 | std_luminance_quant_tbl = std_luminance_quant_tbl.reshape([8,8]) 27 | 28 | std_chrominance_quant_tbl = numpy.array( 29 | [ 17, 18, 24, 47, 99, 99, 99, 99, 30 | 18, 21, 26, 66, 99, 99, 99, 99, 31 | 24, 26, 56, 99, 99, 99, 99, 99, 32 | 47, 66, 99, 99, 99, 99, 99, 99, 33 | 99, 99, 99, 99, 99, 99, 99, 99, 34 | 99, 99, 99, 99, 99, 99, 99, 99, 35 | 99, 99, 99, 99, 99, 99, 99, 99, 36 | 99, 99, 99, 99, 99, 99, 99, 99],dtype=int) 37 | std_chrominance_quant_tbl = std_chrominance_quant_tbl.reshape([8,8]) 38 | 39 | def main(): 40 | 41 | # inputBMPFileName outputJPEGFilename quality(from 1 to 100) DEBUGMODE(0 or 1) 42 | # example: 43 | # ./lena.bmp ./output.jpg 80 0 44 | 45 | if(len(sys.argv)!=5): 46 | print('inputBMPFileName outputJPEGFilename quality(from 1 to 100) DEBUGMODE(0 or 1)') 47 | print('example:') 48 | print('./lena.bmp ./output.jpg 80 0') 49 | return 50 | 51 | srcFileName = sys.argv[1] 52 | outputJPEGFileName = sys.argv[2] 53 | quality = float(sys.argv[3]) 54 | DEBUG_MODE = int(sys.argv[4]) 55 | 56 | 57 | numpy.set_printoptions(threshold=numpy.inf) 58 | srcImage = Image.open(srcFileName) 59 | srcImageWidth, srcImageHeight = srcImage.size 60 | print('srcImageWidth = %d srcImageHeight = %d' % (srcImageWidth, srcImageHeight)) 61 | print('srcImage info:\n', srcImage) 62 | srcImageMatrix = numpy.asarray(srcImage) 63 | 64 | imageWidth = srcImageWidth 65 | imageHeight = srcImageHeight 66 | # add width and height to %8==0 67 | if (srcImageWidth % 8 != 0): 68 | imageWidth = srcImageWidth // 8 * 8 + 8 69 | if (srcImageHeight % 8 != 0): 70 | imageHeight = srcImageHeight // 8 * 8 + 8 71 | 72 | print('added to: ', imageWidth, imageHeight) 73 | 74 | # copy data from srcImageMatrix to addedImageMatrix 75 | addedImageMatrix = numpy.zeros((imageHeight, imageWidth, 3), dtype=numpy.uint8) 76 | for y in range(srcImageHeight): 77 | for x in range(srcImageWidth): 78 | addedImageMatrix[y][x] = srcImageMatrix[y][x] 79 | 80 | 81 | # split y u v 82 | yImage,uImage,vImage = Image.fromarray(addedImageMatrix).convert('YCbCr').split() 83 | 84 | yImageMatrix = numpy.asarray(yImage).astype(int) 85 | uImageMatrix = numpy.asarray(uImage).astype(int) 86 | vImageMatrix = numpy.asarray(vImage).astype(int) 87 | if(DEBUG_MODE==1): 88 | print('yImageMatrix:\n', yImageMatrix) 89 | print('uImageMatrix:\n', uImageMatrix) 90 | print('vImageMatrix:\n', vImageMatrix) 91 | 92 | 93 | yImageMatrix = yImageMatrix - 127 94 | uImageMatrix = uImageMatrix - 127 95 | vImageMatrix = vImageMatrix - 127 96 | 97 | 98 | if(quality <= 0): 99 | quality = 1 100 | if(quality > 100): 101 | quality = 100 102 | if(quality < 50): 103 | qualityScale = 5000 / quality 104 | else: 105 | qualityScale = 200 - quality * 2 106 | luminanceQuantTbl = numpy.array(numpy.floor((std_luminance_quant_tbl * qualityScale + 50) / 100)) 107 | luminanceQuantTbl[luminanceQuantTbl == 0] = 1 108 | luminanceQuantTbl[luminanceQuantTbl > 255] = 255 109 | luminanceQuantTbl = luminanceQuantTbl.reshape([8, 8]).astype(int) 110 | print('luminanceQuantTbl:\n', luminanceQuantTbl) 111 | chrominanceQuantTbl = numpy.array(numpy.floor((std_chrominance_quant_tbl * qualityScale + 50) / 100)) 112 | chrominanceQuantTbl[chrominanceQuantTbl == 0] = 1 113 | chrominanceQuantTbl[chrominanceQuantTbl > 255] = 255 114 | chrominanceQuantTbl = chrominanceQuantTbl.reshape([8, 8]).astype(int) 115 | print('chrominanceQuantTbl:\n', chrominanceQuantTbl) 116 | blockSum = imageWidth // 8 * imageHeight // 8 117 | 118 | yDC = numpy.zeros([blockSum], dtype=int) 119 | uDC = numpy.zeros([blockSum], dtype=int) 120 | vDC = numpy.zeros([blockSum], dtype=int) 121 | dyDC = numpy.zeros([blockSum], dtype=int) 122 | duDC = numpy.zeros([blockSum], dtype=int) 123 | dvDC = numpy.zeros([blockSum], dtype=int) 124 | 125 | print('blockSum = ', blockSum) 126 | 127 | 128 | sosBitStream = BitStream() 129 | 130 | blockNum = 0 131 | for y in range(0, imageHeight, 8): 132 | for x in range(0, imageWidth, 8): 133 | print('block (y,x): ',y, x, ' -> ', y + 8, x + 8) 134 | yDctMatrix = fftpack.dct(fftpack.dct(yImageMatrix[y:y + 8, x:x + 8], norm='ortho').T, norm='ortho').T 135 | uDctMatrix = fftpack.dct(fftpack.dct(uImageMatrix[y:y + 8, x:x + 8], norm='ortho').T, norm='ortho').T 136 | vDctMatrix = fftpack.dct(fftpack.dct(vImageMatrix[y:y + 8, x:x + 8], norm='ortho').T, norm='ortho').T 137 | if(blockSum<=8): 138 | print('yDctMatrix:\n',yDctMatrix) 139 | print('uDctMatrix:\n',uDctMatrix) 140 | print('vDctMatrix:\n',vDctMatrix) 141 | 142 | yQuantMatrix = numpy.rint(yDctMatrix / luminanceQuantTbl) 143 | uQuantMatrix = numpy.rint(uDctMatrix / chrominanceQuantTbl) 144 | vQuantMatrix = numpy.rint(vDctMatrix / chrominanceQuantTbl) 145 | if(DEBUG_MODE==1): 146 | print('yQuantMatrix:\n',yQuantMatrix) 147 | print('uQuantMatrix:\n',uQuantMatrix) 148 | print('vQuantMatrix:\n',vQuantMatrix) 149 | 150 | 151 | yZCode = yQuantMatrix.reshape([64])[zigzagOrder] 152 | uZCode = uQuantMatrix.reshape([64])[zigzagOrder] 153 | vZCode = vQuantMatrix.reshape([64])[zigzagOrder] 154 | yZCode = yZCode.astype(numpy.int) 155 | uZCode = uZCode.astype(numpy.int) 156 | vZCode = vZCode.astype(numpy.int) 157 | 158 | 159 | yDC[blockNum] = yZCode[0] 160 | uDC[blockNum] = uZCode[0] 161 | vDC[blockNum] = vZCode[0] 162 | 163 | if(blockNum==0): 164 | dyDC[blockNum] = yDC[blockNum] 165 | duDC[blockNum] = uDC[blockNum] 166 | dvDC[blockNum] = vDC[blockNum] 167 | else: 168 | dyDC[blockNum] = yDC[blockNum] - yDC[blockNum-1] 169 | duDC[blockNum] = uDC[blockNum] - uDC[blockNum-1] 170 | dvDC[blockNum] = vDC[blockNum] - vDC[blockNum-1] 171 | 172 | 173 | 174 | # huffman encode https://www.impulseadventure.com/photo/jpeg-huffman-coding.html 175 | # encode yDC 176 | if(DEBUG_MODE==1): 177 | print("encode dyDC:",dyDC[blockNum]) 178 | sosBitStream.write(huffmanEncode.encodeDCToBoolList(dyDC[blockNum],1, DEBUG_MODE),bool) 179 | # encode yAC 180 | if (DEBUG_MODE == 1): 181 | print("encode yAC:", yZCode[1:]) 182 | huffmanEncode.encodeACBlock(sosBitStream, yZCode[1:], 1, DEBUG_MODE) 183 | 184 | # encode uDC 185 | if(DEBUG_MODE==1): 186 | print("encode duDC:",duDC[blockNum]) 187 | sosBitStream.write(huffmanEncode.encodeDCToBoolList(duDC[blockNum],0, DEBUG_MODE),bool) 188 | # encode uAC 189 | if (DEBUG_MODE == 1): 190 | print("encode uAC:", uZCode[1:]) 191 | huffmanEncode.encodeACBlock(sosBitStream, uZCode[1:], 0, DEBUG_MODE) 192 | 193 | # encode vDC 194 | if(DEBUG_MODE==1): 195 | print("encode dvDC:",dvDC[blockNum]) 196 | sosBitStream.write(huffmanEncode.encodeDCToBoolList(dvDC[blockNum],0, DEBUG_MODE),bool) 197 | # encode uAC 198 | if (DEBUG_MODE == 1): 199 | print("encode vAC:", vZCode[1:]) 200 | huffmanEncode.encodeACBlock(sosBitStream, vZCode[1:], 0, DEBUG_MODE) 201 | 202 | blockNum = blockNum + 1 203 | 204 | 205 | 206 | jpegFile = open(outputJPEGFileName, 'wb+') 207 | # write jpeg header 208 | jpegFile.write(huffmanEncode.hexToBytes('FFD8FFE000104A46494600010100000100010000')) 209 | # write y Quantization Table 210 | jpegFile.write(huffmanEncode.hexToBytes('FFDB004300')) 211 | luminanceQuantTbl = luminanceQuantTbl.reshape([64]) 212 | jpegFile.write(bytes(luminanceQuantTbl.tolist())) 213 | # write u/v Quantization Table 214 | jpegFile.write(huffmanEncode.hexToBytes('FFDB004301')) 215 | chrominanceQuantTbl = chrominanceQuantTbl.reshape([64]) 216 | jpegFile.write(bytes(chrominanceQuantTbl.tolist())) 217 | # write height and width 218 | jpegFile.write(huffmanEncode.hexToBytes('FFC0001108')) 219 | hHex = hex(srcImageHeight)[2:] 220 | while len(hHex) != 4: 221 | hHex = '0' + hHex 222 | 223 | jpegFile.write(huffmanEncode.hexToBytes(hHex)) 224 | 225 | wHex = hex(srcImageWidth)[2:] 226 | while len(wHex) != 4: 227 | wHex = '0' + wHex 228 | 229 | jpegFile.write(huffmanEncode.hexToBytes(wHex)) 230 | 231 | # 03 01 11 00 02 11 01 03 11 01 232 | # 1:1 01 11 00 02 11 01 03 11 01 233 | # 1:2 01 21 00 02 11 01 03 11 01 234 | # 1:4 01 22 00 02 11 01 03 11 01 235 | # write Subsamp 236 | jpegFile.write(huffmanEncode.hexToBytes('03011100021101031101')) 237 | 238 | #write huffman table 239 | jpegFile.write(huffmanEncode.hexToBytes('FFC401A20000000701010101010000000000000000040503020601000708090A0B0100020203010101010100000000000000010002030405060708090A0B1000020103030204020607030402060273010203110400052112314151061361227181143291A10715B14223C152D1E1331662F0247282F12543345392A2B26373C235442793A3B33617546474C3D2E2082683090A181984944546A4B456D355281AF2E3F3C4D4E4F465758595A5B5C5D5E5F566768696A6B6C6D6E6F637475767778797A7B7C7D7E7F738485868788898A8B8C8D8E8F82939495969798999A9B9C9D9E9F92A3A4A5A6A7A8A9AAABACADAEAFA110002020102030505040506040803036D0100021103042112314105511361220671819132A1B1F014C1D1E1234215526272F1332434438216925325A263B2C20773D235E2448317549308090A18192636451A2764745537F2A3B3C32829D3E3F38494A4B4C4D4E4F465758595A5B5C5D5E5F5465666768696A6B6C6D6E6F6475767778797A7B7C7D7E7F738485868788898A8B8C8D8E8F839495969798999A9B9C9D9E9F92A3A4A5A6A7A8A9AAABACADAEAFA')) 240 | # SOS Start of Scan 241 | # yDC yAC uDC uAC vDC vAC 242 | sosLength = sosBitStream.__len__() 243 | filledNum = 8 - sosLength % 8 244 | if(filledNum!=0): 245 | sosBitStream.write(numpy.ones([filledNum]).tolist(),bool) 246 | 247 | jpegFile.write(bytes([255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0])) # FF DA 00 0C 03 01 00 02 11 03 11 00 3F 00 248 | 249 | # write encoded data 250 | sosBytes = sosBitStream.read(bytes) 251 | for i in range(len(sosBytes)): 252 | jpegFile.write(bytes([sosBytes[i]])) 253 | if(sosBytes[i]==255): 254 | jpegFile.write(bytes([0])) # FF to FF 00 255 | 256 | # write end symbol 257 | jpegFile.write(bytes([255,217])) # FF D9 258 | jpegFile.close() 259 | 260 | 261 | if __name__ == '__main__': 262 | main() 263 | 264 | 265 | 266 | -------------------------------------------------------------------------------- /lena.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fangwei123456/python-jpeg-encoder/d92dcf7718543d9f09c0df43bb7df2e6ada76394/lena.bmp --------------------------------------------------------------------------------