Source Code Cross Referenced for TIFFFaxDecoder.java in  » Graphic-Library » xmlgraphics-commons-1.2 » org » apache » xmlgraphics » image » codec » tiff » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Graphic Library » xmlgraphics commons 1.2 » org.apache.xmlgraphics.image.codec.tiff 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         * 
0009:         *      http://www.apache.org/licenses/LICENSE-2.0
0010:         * 
0011:         * Unless required by applicable law or agreed to in writing, software
0012:         * distributed under the License is distributed on an "AS IS" BASIS,
0013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         * See the License for the specific language governing permissions and
0015:         * limitations under the License.
0016:         */
0017:
0018:        /* $Id: TIFFFaxDecoder.java 496559 2007-01-16 01:10:29Z cam $ */
0019:
0020:        package org.apache.xmlgraphics.image.codec.tiff;
0021:
0022:        class TIFFFaxDecoder {
0023:
0024:            private int bitPointer, bytePointer;
0025:            private byte[] data;
0026:            private int w, h;
0027:            private int fillOrder;
0028:
0029:            // Data structures needed to store changing elements for the previous
0030:            // and the current scanline
0031:            private int changingElemSize = 0;
0032:            private int[] prevChangingElems;
0033:            private int[] currChangingElems;
0034:
0035:            // Element at which to start search in getNextChangingElement
0036:            private int lastChangingElement = 0;
0037:
0038:            private int compression = 2;
0039:
0040:            // Variables set by T4Options
0041:            private int uncompressedMode = 0;
0042:            private int fillBits = 0;
0043:            private int oneD;
0044:
0045:            static int[] table1 = { 0x00, // 0 bits are left in first byte - SHOULD NOT HAPPEN
0046:                    0x01, // 1 bits are left in first byte
0047:                    0x03, // 2 bits are left in first byte
0048:                    0x07, // 3 bits are left in first byte
0049:                    0x0f, // 4 bits are left in first byte
0050:                    0x1f, // 5 bits are left in first byte
0051:                    0x3f, // 6 bits are left in first byte
0052:                    0x7f, // 7 bits are left in first byte
0053:                    0xff // 8 bits are left in first byte
0054:            };
0055:
0056:            static int[] table2 = { 0x00, // 0
0057:                    0x80, // 1
0058:                    0xc0, // 2
0059:                    0xe0, // 3
0060:                    0xf0, // 4
0061:                    0xf8, // 5
0062:                    0xfc, // 6
0063:                    0xfe, // 7
0064:                    0xff // 8
0065:            };
0066:
0067:            // Table to be used when fillOrder = 2, for flipping bytes.
0068:            static byte[] flipTable = { 0, -128, 64, -64, 32, -96, 96, -32, 16,
0069:                    -112, 80, -48, 48, -80, 112, -16, 8, -120, 72, -56, 40,
0070:                    -88, 104, -24, 24, -104, 88, -40, 56, -72, 120, -8, 4,
0071:                    -124, 68, -60, 36, -92, 100, -28, 20, -108, 84, -44, 52,
0072:                    -76, 116, -12, 12, -116, 76, -52, 44, -84, 108, -20, 28,
0073:                    -100, 92, -36, 60, -68, 124, -4, 2, -126, 66, -62, 34, -94,
0074:                    98, -30, 18, -110, 82, -46, 50, -78, 114, -14, 10, -118,
0075:                    74, -54, 42, -86, 106, -22, 26, -102, 90, -38, 58, -70,
0076:                    122, -6, 6, -122, 70, -58, 38, -90, 102, -26, 22, -106, 86,
0077:                    -42, 54, -74, 118, -10, 14, -114, 78, -50, 46, -82, 110,
0078:                    -18, 30, -98, 94, -34, 62, -66, 126, -2, 1, -127, 65, -63,
0079:                    33, -95, 97, -31, 17, -111, 81, -47, 49, -79, 113, -15, 9,
0080:                    -119, 73, -55, 41, -87, 105, -23, 25, -103, 89, -39, 57,
0081:                    -71, 121, -7, 5, -123, 69, -59, 37, -91, 101, -27, 21,
0082:                    -107, 85, -43, 53, -75, 117, -11, 13, -115, 77, -51, 45,
0083:                    -83, 109, -19, 29, -99, 93, -35, 61, -67, 125, -3, 3, -125,
0084:                    67, -61, 35, -93, 99, -29, 19, -109, 83, -45, 51, -77, 115,
0085:                    -13, 11, -117, 75, -53, 43, -85, 107, -21, 27, -101, 91,
0086:                    -37, 59, -69, 123, -5, 7, -121, 71, -57, 39, -89, 103, -25,
0087:                    23, -105, 87, -41, 55, -73, 119, -9, 15, -113, 79, -49, 47,
0088:                    -81, 111, -17, 31, -97, 95, -33, 63, -65, 127, -1, };
0089:
0090:            // The main 10 bit white runs lookup table
0091:            static short[] white = {
0092:                    // 0 - 7
0093:                    6430, 6400, 6400, 6400,
0094:                    3225,
0095:                    3225,
0096:                    3225,
0097:                    3225,
0098:                    // 8 - 15
0099:                    944, 944, 944, 944,
0100:                    976,
0101:                    976,
0102:                    976,
0103:                    976,
0104:                    // 16 - 23
0105:                    1456, 1456, 1456, 1456,
0106:                    1488,
0107:                    1488,
0108:                    1488,
0109:                    1488,
0110:                    // 24 - 31
0111:                    718, 718, 718, 718,
0112:                    718,
0113:                    718,
0114:                    718,
0115:                    718,
0116:                    // 32 - 39
0117:                    750, 750, 750, 750,
0118:                    750,
0119:                    750,
0120:                    750,
0121:                    750,
0122:                    // 40 - 47
0123:                    1520, 1520, 1520, 1520,
0124:                    1552,
0125:                    1552,
0126:                    1552,
0127:                    1552,
0128:                    // 48 - 55
0129:                    428, 428, 428, 428,
0130:                    428,
0131:                    428,
0132:                    428,
0133:                    428,
0134:                    // 56 - 63
0135:                    428, 428, 428, 428,
0136:                    428,
0137:                    428,
0138:                    428,
0139:                    428,
0140:                    // 64 - 71
0141:                    654, 654, 654, 654,
0142:                    654,
0143:                    654,
0144:                    654,
0145:                    654,
0146:                    // 72 - 79
0147:                    1072, 1072, 1072, 1072,
0148:                    1104,
0149:                    1104,
0150:                    1104,
0151:                    1104,
0152:                    // 80 - 87
0153:                    1136, 1136, 1136, 1136,
0154:                    1168,
0155:                    1168,
0156:                    1168,
0157:                    1168,
0158:                    // 88 - 95
0159:                    1200, 1200, 1200, 1200,
0160:                    1232,
0161:                    1232,
0162:                    1232,
0163:                    1232,
0164:                    // 96 - 103
0165:                    622, 622, 622, 622,
0166:                    622,
0167:                    622,
0168:                    622,
0169:                    622,
0170:                    // 104 - 111
0171:                    1008, 1008, 1008, 1008,
0172:                    1040,
0173:                    1040,
0174:                    1040,
0175:                    1040,
0176:                    // 112 - 119
0177:                    44, 44, 44, 44,
0178:                    44,
0179:                    44,
0180:                    44,
0181:                    44,
0182:                    // 120 - 127
0183:                    44, 44, 44, 44,
0184:                    44,
0185:                    44,
0186:                    44,
0187:                    44,
0188:                    // 128 - 135
0189:                    396, 396, 396, 396,
0190:                    396,
0191:                    396,
0192:                    396,
0193:                    396,
0194:                    // 136 - 143
0195:                    396, 396, 396, 396,
0196:                    396,
0197:                    396,
0198:                    396,
0199:                    396,
0200:                    // 144 - 151
0201:                    1712, 1712, 1712, 1712,
0202:                    1744,
0203:                    1744,
0204:                    1744,
0205:                    1744,
0206:                    // 152 - 159
0207:                    846, 846, 846, 846,
0208:                    846,
0209:                    846,
0210:                    846,
0211:                    846,
0212:                    // 160 - 167
0213:                    1264, 1264, 1264, 1264,
0214:                    1296,
0215:                    1296,
0216:                    1296,
0217:                    1296,
0218:                    // 168 - 175
0219:                    1328, 1328, 1328, 1328,
0220:                    1360,
0221:                    1360,
0222:                    1360,
0223:                    1360,
0224:                    // 176 - 183
0225:                    1392, 1392, 1392, 1392,
0226:                    1424,
0227:                    1424,
0228:                    1424,
0229:                    1424,
0230:                    // 184 - 191
0231:                    686, 686, 686, 686,
0232:                    686,
0233:                    686,
0234:                    686,
0235:                    686,
0236:                    // 192 - 199
0237:                    910, 910, 910, 910,
0238:                    910,
0239:                    910,
0240:                    910,
0241:                    910,
0242:                    // 200 - 207
0243:                    1968, 1968, 1968, 1968,
0244:                    2000,
0245:                    2000,
0246:                    2000,
0247:                    2000,
0248:                    // 208 - 215
0249:                    2032, 2032, 2032, 2032,
0250:                    16,
0251:                    16,
0252:                    16,
0253:                    16,
0254:                    // 216 - 223
0255:                    10257, 10257, 10257, 10257,
0256:                    12305,
0257:                    12305,
0258:                    12305,
0259:                    12305,
0260:                    // 224 - 231
0261:                    330, 330, 330, 330,
0262:                    330,
0263:                    330,
0264:                    330,
0265:                    330,
0266:                    // 232 - 239
0267:                    330, 330, 330, 330,
0268:                    330,
0269:                    330,
0270:                    330,
0271:                    330,
0272:                    // 240 - 247
0273:                    330, 330, 330, 330,
0274:                    330,
0275:                    330,
0276:                    330,
0277:                    330,
0278:                    // 248 - 255
0279:                    330, 330, 330, 330,
0280:                    330,
0281:                    330,
0282:                    330,
0283:                    330,
0284:                    // 256 - 263
0285:                    362, 362, 362, 362,
0286:                    362,
0287:                    362,
0288:                    362,
0289:                    362,
0290:                    // 264 - 271
0291:                    362, 362, 362, 362,
0292:                    362,
0293:                    362,
0294:                    362,
0295:                    362,
0296:                    // 272 - 279
0297:                    362, 362, 362, 362,
0298:                    362,
0299:                    362,
0300:                    362,
0301:                    362,
0302:                    // 280 - 287
0303:                    362, 362, 362, 362,
0304:                    362,
0305:                    362,
0306:                    362,
0307:                    362,
0308:                    // 288 - 295
0309:                    878, 878, 878, 878,
0310:                    878,
0311:                    878,
0312:                    878,
0313:                    878,
0314:                    // 296 - 303
0315:                    1904, 1904, 1904, 1904,
0316:                    1936,
0317:                    1936,
0318:                    1936,
0319:                    1936,
0320:                    // 304 - 311
0321:                    -18413, -18413, -16365, -16365,
0322:                    -14317,
0323:                    -14317,
0324:                    -10221,
0325:                    -10221,
0326:                    // 312 - 319
0327:                    590, 590, 590, 590, 590,
0328:                    590,
0329:                    590,
0330:                    590,
0331:                    // 320 - 327
0332:                    782, 782, 782, 782, 782,
0333:                    782,
0334:                    782,
0335:                    782,
0336:                    // 328 - 335
0337:                    1584, 1584, 1584, 1584, 1616,
0338:                    1616,
0339:                    1616,
0340:                    1616,
0341:                    // 336 - 343
0342:                    1648, 1648, 1648, 1648, 1680,
0343:                    1680,
0344:                    1680,
0345:                    1680,
0346:                    // 344 - 351
0347:                    814, 814, 814, 814, 814,
0348:                    814,
0349:                    814,
0350:                    814,
0351:                    // 352 - 359
0352:                    1776, 1776, 1776, 1776, 1808,
0353:                    1808,
0354:                    1808,
0355:                    1808,
0356:                    // 360 - 367
0357:                    1840, 1840, 1840, 1840, 1872,
0358:                    1872,
0359:                    1872,
0360:                    1872,
0361:                    // 368 - 375
0362:                    6157, 6157, 6157, 6157, 6157,
0363:                    6157,
0364:                    6157,
0365:                    6157,
0366:                    // 376 - 383
0367:                    6157, 6157, 6157, 6157, 6157,
0368:                    6157,
0369:                    6157,
0370:                    6157,
0371:                    // 384 - 391
0372:                    -12275, -12275, -12275, -12275, -12275,
0373:                    -12275,
0374:                    -12275,
0375:                    -12275,
0376:                    // 392 - 399
0377:                    -12275, -12275, -12275, -12275, -12275, -12275,
0378:                    -12275,
0379:                    -12275,
0380:                    // 400 - 407
0381:                    14353, 14353, 14353, 14353, 16401, 16401, 16401,
0382:                    16401,
0383:                    // 408 - 415
0384:                    22547, 22547, 24595, 24595, 20497, 20497, 20497,
0385:                    20497,
0386:                    // 416 - 423
0387:                    18449, 18449, 18449, 18449, 26643, 26643, 28691,
0388:                    28691,
0389:                    // 424 - 431
0390:                    30739, 30739, -32749, -32749, -30701, -30701, -28653,
0391:                    -28653,
0392:                    // 432 - 439
0393:                    -26605, -26605, -24557, -24557, -22509, -22509, -20461,
0394:                    -20461,
0395:                    // 440 - 447
0396:                    8207, 8207, 8207, 8207, 8207, 8207, 8207, 8207,
0397:                    // 448 - 455
0398:                    72, 72, 72, 72, 72, 72, 72, 72,
0399:                    // 456 - 463
0400:                    72, 72, 72, 72, 72, 72, 72, 72,
0401:                    // 464 - 471
0402:                    72, 72, 72, 72, 72, 72, 72, 72,
0403:                    // 472 - 479
0404:                    72, 72, 72, 72, 72, 72, 72, 72,
0405:                    // 480 - 487
0406:                    72, 72, 72, 72, 72, 72, 72, 72,
0407:                    // 488 - 495
0408:                    72, 72, 72, 72, 72, 72, 72, 72,
0409:                    // 496 - 503
0410:                    72, 72, 72, 72, 72, 72, 72, 72,
0411:                    // 504 - 511
0412:                    72, 72, 72, 72, 72, 72, 72, 72,
0413:                    // 512 - 519
0414:                    104, 104, 104, 104, 104, 104, 104, 104,
0415:                    // 520 - 527
0416:                    104, 104, 104, 104, 104, 104, 104, 104,
0417:                    // 528 - 535
0418:                    104, 104, 104, 104, 104, 104, 104, 104,
0419:                    // 536 - 543
0420:                    104, 104, 104, 104, 104, 104, 104, 104,
0421:                    // 544 - 551
0422:                    104, 104, 104, 104, 104, 104, 104, 104,
0423:                    // 552 - 559
0424:                    104, 104, 104, 104, 104, 104, 104, 104,
0425:                    // 560 - 567
0426:                    104, 104, 104, 104, 104, 104, 104, 104,
0427:                    // 568 - 575
0428:                    104, 104, 104, 104, 104, 104, 104, 104,
0429:                    // 576 - 583
0430:                    4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
0431:                    // 584 - 591
0432:                    4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
0433:                    // 592 - 599
0434:                    4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
0435:                    // 600 - 607
0436:                    4107, 4107, 4107, 4107, 4107, 4107, 4107, 4107,
0437:                    // 608 - 615
0438:                    266, 266, 266, 266, 266, 266, 266, 266,
0439:                    // 616 - 623
0440:                    266, 266, 266, 266, 266, 266, 266, 266,
0441:                    // 624 - 631
0442:                    266, 266, 266, 266, 266, 266, 266, 266,
0443:                    // 632 - 639
0444:                    266, 266, 266, 266, 266, 266, 266, 266,
0445:                    // 640 - 647
0446:                    298, 298, 298, 298, 298, 298, 298, 298,
0447:                    // 648 - 655
0448:                    298, 298, 298, 298, 298, 298, 298, 298,
0449:                    // 656 - 663
0450:                    298, 298, 298, 298, 298, 298, 298, 298,
0451:                    // 664 - 671
0452:                    298, 298, 298, 298, 298, 298, 298, 298,
0453:                    // 672 - 679
0454:                    524, 524, 524, 524, 524, 524, 524, 524,
0455:                    // 680 - 687
0456:                    524, 524, 524, 524, 524, 524, 524, 524,
0457:                    // 688 - 695
0458:                    556, 556, 556, 556, 556, 556, 556, 556,
0459:                    // 696 - 703
0460:                    556, 556, 556, 556, 556, 556, 556, 556,
0461:                    // 704 - 711
0462:                    136, 136, 136, 136, 136, 136, 136, 136,
0463:                    // 712 - 719
0464:                    136, 136, 136, 136, 136, 136, 136, 136,
0465:                    // 720 - 727
0466:                    136, 136, 136, 136, 136, 136, 136, 136,
0467:                    // 728 - 735
0468:                    136, 136, 136, 136, 136, 136, 136, 136,
0469:                    // 736 - 743
0470:                    136, 136, 136, 136, 136, 136, 136, 136,
0471:                    // 744 - 751
0472:                    136, 136, 136, 136, 136, 136, 136, 136,
0473:                    // 752 - 759
0474:                    136, 136, 136, 136, 136, 136, 136, 136,
0475:                    // 760 - 767
0476:                    136, 136, 136, 136, 136, 136, 136, 136,
0477:                    // 768 - 775
0478:                    168, 168, 168, 168, 168, 168, 168, 168,
0479:                    // 776 - 783
0480:                    168, 168, 168, 168, 168, 168, 168, 168,
0481:                    // 784 - 791
0482:                    168, 168, 168, 168, 168, 168, 168, 168,
0483:                    // 792 - 799
0484:                    168, 168, 168, 168, 168, 168, 168, 168,
0485:                    // 800 - 807
0486:                    168, 168, 168, 168, 168, 168, 168, 168,
0487:                    // 808 - 815
0488:                    168, 168, 168, 168, 168, 168, 168, 168,
0489:                    // 816 - 823
0490:                    168, 168, 168, 168, 168, 168, 168, 168,
0491:                    // 824 - 831
0492:                    168, 168, 168, 168, 168, 168, 168, 168,
0493:                    // 832 - 839
0494:                    460, 460, 460, 460, 460, 460, 460, 460,
0495:                    // 840 - 847
0496:                    460, 460, 460, 460, 460, 460, 460, 460,
0497:                    // 848 - 855
0498:                    492, 492, 492, 492, 492, 492, 492, 492,
0499:                    // 856 - 863
0500:                    492, 492, 492, 492, 492, 492, 492, 492,
0501:                    // 864 - 871
0502:                    2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
0503:                    // 872 - 879
0504:                    2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
0505:                    // 880 - 887
0506:                    2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
0507:                    // 888 - 895
0508:                    2059, 2059, 2059, 2059, 2059, 2059, 2059, 2059,
0509:                    // 896 - 903
0510:                    200, 200, 200, 200, 200, 200, 200, 200,
0511:                    // 904 - 911
0512:                    200, 200, 200, 200, 200, 200, 200, 200,
0513:                    // 912 - 919
0514:                    200, 200, 200, 200, 200, 200, 200, 200,
0515:                    // 920 - 927
0516:                    200, 200, 200, 200, 200, 200, 200, 200,
0517:                    // 928 - 935
0518:                    200, 200, 200, 200, 200, 200, 200, 200,
0519:                    // 936 - 943
0520:                    200, 200, 200, 200, 200, 200, 200, 200,
0521:                    // 944 - 951
0522:                    200, 200, 200, 200, 200, 200, 200, 200,
0523:                    // 952 - 959
0524:                    200, 200, 200, 200, 200, 200, 200, 200,
0525:                    // 960 - 967
0526:                    232, 232, 232, 232, 232, 232, 232, 232,
0527:                    // 968 - 975
0528:                    232, 232, 232, 232, 232, 232, 232, 232,
0529:                    // 976 - 983
0530:                    232, 232, 232, 232, 232, 232, 232, 232,
0531:                    // 984 - 991
0532:                    232, 232, 232, 232, 232, 232, 232, 232,
0533:                    // 992 - 999
0534:                    232, 232, 232, 232, 232, 232, 232, 232,
0535:                    // 1000 - 1007
0536:                    232, 232, 232, 232, 232, 232, 232, 232,
0537:                    // 1008 - 1015
0538:                    232, 232, 232, 232, 232, 232, 232, 232,
0539:                    // 1016 - 1023
0540:                    232, 232, 232, 232, 232, 232, 232, 232, };
0541:
0542:            // Additional make up codes for both White and Black runs
0543:            static short[] additionalMakeup = { 28679, 28679, 31752,
0544:                    (short) 32777, (short) 33801, (short) 34825, (short) 35849,
0545:                    (short) 36873, (short) 29703, (short) 29703, (short) 30727,
0546:                    (short) 30727, (short) 37897, (short) 38921, (short) 39945,
0547:                    (short) 40969 };
0548:
0549:            // Initial black run look up table, uses the first 4 bits of a code
0550:            static short[] initBlack = {
0551:            // 0 - 7
0552:                    3226, 6412, 200, 168, 38, 38, 134, 134,
0553:                    // 8 - 15
0554:                    100, 100, 100, 100, 68, 68, 68, 68 };
0555:
0556:            //
0557:            static short[] twoBitBlack = { 292, 260, 226, 226 }; // 0 - 3
0558:
0559:            // Main black run table, using the last 9 bits of possible 13 bit code
0560:            static short[] black = {
0561:            // 0 - 7
0562:                    62, 62, 30, 30, 0, 0, 0, 0,
0563:                    // 8 - 15
0564:                    0, 0, 0, 0, 0, 0, 0, 0,
0565:                    // 16 - 23
0566:                    0, 0, 0, 0, 0, 0, 0, 0,
0567:                    // 24 - 31
0568:                    0, 0, 0, 0, 0, 0, 0, 0,
0569:                    // 32 - 39
0570:                    3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
0571:                    // 40 - 47
0572:                    3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
0573:                    // 48 - 55
0574:                    3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
0575:                    // 56 - 63
0576:                    3225, 3225, 3225, 3225, 3225, 3225, 3225, 3225,
0577:                    // 64 - 71
0578:                    588, 588, 588, 588, 588, 588, 588, 588,
0579:                    // 72 - 79
0580:                    1680, 1680, 20499, 22547, 24595, 26643, 1776, 1776,
0581:                    // 80 - 87
0582:                    1808, 1808, -24557, -22509, -20461, -18413, 1904, 1904,
0583:                    // 88 - 95
0584:                    1936, 1936, -16365, -14317, 782, 782, 782, 782,
0585:                    // 96 - 103
0586:                    814, 814, 814, 814, -12269, -10221, 10257, 10257,
0587:                    // 104 - 111
0588:                    12305, 12305, 14353, 14353, 16403, 18451, 1712, 1712,
0589:                    // 112 - 119
0590:                    1744, 1744, 28691, 30739, -32749, -30701, -28653, -26605,
0591:                    // 120 - 127
0592:                    2061, 2061, 2061, 2061, 2061, 2061, 2061, 2061,
0593:                    // 128 - 135
0594:                    424, 424, 424, 424, 424, 424, 424, 424,
0595:                    // 136 - 143
0596:                    424, 424, 424, 424, 424, 424, 424, 424,
0597:                    // 144 - 151
0598:                    424, 424, 424, 424, 424, 424, 424, 424,
0599:                    // 152 - 159
0600:                    424, 424, 424, 424, 424, 424, 424, 424,
0601:                    // 160 - 167
0602:                    750, 750, 750, 750, 1616, 1616, 1648, 1648,
0603:                    // 168 - 175
0604:                    1424, 1424, 1456, 1456, 1488, 1488, 1520, 1520,
0605:                    // 176 - 183
0606:                    1840, 1840, 1872, 1872, 1968, 1968, 8209, 8209,
0607:                    // 184 - 191
0608:                    524, 524, 524, 524, 524, 524, 524, 524,
0609:                    // 192 - 199
0610:                    556, 556, 556, 556, 556, 556, 556, 556,
0611:                    // 200 - 207
0612:                    1552, 1552, 1584, 1584, 2000, 2000, 2032, 2032,
0613:                    // 208 - 215
0614:                    976, 976, 1008, 1008, 1040, 1040, 1072, 1072,
0615:                    // 216 - 223
0616:                    1296, 1296, 1328, 1328, 718, 718, 718, 718,
0617:                    // 224 - 231
0618:                    456, 456, 456, 456, 456, 456, 456, 456,
0619:                    // 232 - 239
0620:                    456, 456, 456, 456, 456, 456, 456, 456,
0621:                    // 240 - 247
0622:                    456, 456, 456, 456, 456, 456, 456, 456,
0623:                    // 248 - 255
0624:                    456, 456, 456, 456, 456, 456, 456, 456,
0625:                    // 256 - 263
0626:                    326, 326, 326, 326, 326, 326, 326, 326,
0627:                    // 264 - 271
0628:                    326, 326, 326, 326, 326, 326, 326, 326,
0629:                    // 272 - 279
0630:                    326, 326, 326, 326, 326, 326, 326, 326,
0631:                    // 280 - 287
0632:                    326, 326, 326, 326, 326, 326, 326, 326,
0633:                    // 288 - 295
0634:                    326, 326, 326, 326, 326, 326, 326, 326,
0635:                    // 296 - 303
0636:                    326, 326, 326, 326, 326, 326, 326, 326,
0637:                    // 304 - 311
0638:                    326, 326, 326, 326, 326, 326, 326, 326,
0639:                    // 312 - 319
0640:                    326, 326, 326, 326, 326, 326, 326, 326,
0641:                    // 320 - 327
0642:                    358, 358, 358, 358, 358, 358, 358, 358,
0643:                    // 328 - 335
0644:                    358, 358, 358, 358, 358, 358, 358, 358,
0645:                    // 336 - 343
0646:                    358, 358, 358, 358, 358, 358, 358, 358,
0647:                    // 344 - 351
0648:                    358, 358, 358, 358, 358, 358, 358, 358,
0649:                    // 352 - 359
0650:                    358, 358, 358, 358, 358, 358, 358, 358,
0651:                    // 360 - 367
0652:                    358, 358, 358, 358, 358, 358, 358, 358,
0653:                    // 368 - 375
0654:                    358, 358, 358, 358, 358, 358, 358, 358,
0655:                    // 376 - 383
0656:                    358, 358, 358, 358, 358, 358, 358, 358,
0657:                    // 384 - 391
0658:                    490, 490, 490, 490, 490, 490, 490, 490,
0659:                    // 392 - 399
0660:                    490, 490, 490, 490, 490, 490, 490, 490,
0661:                    // 400 - 407
0662:                    4113, 4113, 6161, 6161, 848, 848, 880, 880,
0663:                    // 408 - 415
0664:                    912, 912, 944, 944, 622, 622, 622, 622,
0665:                    // 416 - 423
0666:                    654, 654, 654, 654, 1104, 1104, 1136, 1136,
0667:                    // 424 - 431
0668:                    1168, 1168, 1200, 1200, 1232, 1232, 1264, 1264,
0669:                    // 432 - 439
0670:                    686, 686, 686, 686, 1360, 1360, 1392, 1392,
0671:                    // 440 - 447
0672:                    12, 12, 12, 12, 12, 12, 12, 12,
0673:                    // 448 - 455
0674:                    390, 390, 390, 390, 390, 390, 390, 390,
0675:                    // 456 - 463
0676:                    390, 390, 390, 390, 390, 390, 390, 390,
0677:                    // 464 - 471
0678:                    390, 390, 390, 390, 390, 390, 390, 390,
0679:                    // 472 - 479
0680:                    390, 390, 390, 390, 390, 390, 390, 390,
0681:                    // 480 - 487
0682:                    390, 390, 390, 390, 390, 390, 390, 390,
0683:                    // 488 - 495
0684:                    390, 390, 390, 390, 390, 390, 390, 390,
0685:                    // 496 - 503
0686:                    390, 390, 390, 390, 390, 390, 390, 390,
0687:                    // 504 - 511
0688:                    390, 390, 390, 390, 390, 390, 390, 390, };
0689:
0690:            static byte[] twoDCodes = {
0691:            // 0 - 7
0692:                    80, 88, 23, 71, 30, 30, 62, 62,
0693:                    // 8 - 15
0694:                    4, 4, 4, 4, 4, 4, 4, 4,
0695:                    // 16 - 23
0696:                    11, 11, 11, 11, 11, 11, 11, 11,
0697:                    // 24 - 31
0698:                    11, 11, 11, 11, 11, 11, 11, 11,
0699:                    // 32 - 39
0700:                    35, 35, 35, 35, 35, 35, 35, 35,
0701:                    // 40 - 47
0702:                    35, 35, 35, 35, 35, 35, 35, 35,
0703:                    // 48 - 55
0704:                    51, 51, 51, 51, 51, 51, 51, 51,
0705:                    // 56 - 63
0706:                    51, 51, 51, 51, 51, 51, 51, 51,
0707:                    // 64 - 71
0708:                    41, 41, 41, 41, 41, 41, 41, 41,
0709:                    // 72 - 79
0710:                    41, 41, 41, 41, 41, 41, 41, 41,
0711:                    // 80 - 87
0712:                    41, 41, 41, 41, 41, 41, 41, 41,
0713:                    // 88 - 95
0714:                    41, 41, 41, 41, 41, 41, 41, 41,
0715:                    // 96 - 103
0716:                    41, 41, 41, 41, 41, 41, 41, 41,
0717:                    // 104 - 111
0718:                    41, 41, 41, 41, 41, 41, 41, 41,
0719:                    // 112 - 119
0720:                    41, 41, 41, 41, 41, 41, 41, 41,
0721:                    // 120 - 127
0722:                    41, 41, 41, 41, 41, 41, 41, 41, };
0723:
0724:            /**
0725:             * @param fillOrder   The fill order of the compressed data bytes.
0726:             * @param w The width of the image in pixels
0727:             * @param h The height of the image in pixels
0728:             */
0729:            public TIFFFaxDecoder(int fillOrder, int w, int h) {
0730:                this .fillOrder = fillOrder;
0731:                this .w = w;
0732:                this .h = h;
0733:
0734:                this .bitPointer = 0;
0735:                this .bytePointer = 0;
0736:                this .prevChangingElems = new int[w];
0737:                this .currChangingElems = new int[w];
0738:            }
0739:
0740:            // One-dimensional decoding methods
0741:
0742:            public void decode1D(byte[] buffer, byte[] compData, int startX,
0743:                    int height) {
0744:                this .data = compData;
0745:
0746:                int lineOffset = 0;
0747:                int scanlineStride = (w + 7) / 8;
0748:
0749:                bitPointer = 0;
0750:                bytePointer = 0;
0751:
0752:                for (int i = 0; i < height; i++) {
0753:                    decodeNextScanline(buffer, lineOffset, startX);
0754:                    lineOffset += scanlineStride;
0755:                }
0756:            }
0757:
0758:            public void decodeNextScanline(byte[] buffer, int lineOffset,
0759:                    int bitOffset) {
0760:                int bits = 0, code = 0, isT = 0;
0761:                int current, entry, twoBits;
0762:                boolean isWhite = true;
0763:
0764:                // Initialize starting of the changing elements array
0765:                changingElemSize = 0;
0766:
0767:                // While scanline not complete
0768:                while (bitOffset < w) {
0769:                    while (isWhite) {
0770:                        // White run
0771:                        current = nextNBits(10);
0772:                        entry = white[current];
0773:
0774:                        // Get the 3 fields from the entry
0775:                        isT = entry & 0x0001;
0776:                        bits = (entry >>> 1) & 0x0f;
0777:
0778:                        if (bits == 12) { // Additional Make up code
0779:                            // Get the next 2 bits
0780:                            twoBits = nextLesserThan8Bits(2);
0781:                            // Consolidate the 2 new bits and last 2 bits into 4 bits
0782:                            current = ((current << 2) & 0x000c) | twoBits;
0783:                            entry = additionalMakeup[current];
0784:                            bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
0785:                            code = (entry >>> 4) & 0x0fff; // 12 bits
0786:                            bitOffset += code; // Skip white run
0787:
0788:                            updatePointer(4 - bits);
0789:                        } else if (bits == 0) { // ERROR
0790:                            throw new Error("TIFFFaxDecoder0");
0791:                        } else if (bits == 15) { // EOL
0792:                            throw new Error("TIFFFaxDecoder1");
0793:                        } else {
0794:                            // 11 bits - 0000 0111 1111 1111 = 0x07ff
0795:                            code = (entry >>> 5) & 0x07ff;
0796:                            bitOffset += code;
0797:
0798:                            updatePointer(10 - bits);
0799:                            if (isT == 0) {
0800:                                isWhite = false;
0801:                                currChangingElems[changingElemSize++] = bitOffset;
0802:                            }
0803:                        }
0804:                    }
0805:
0806:                    // Check whether this run completed one width, if so
0807:                    // advance to next byte boundary for compression = 2.
0808:                    if (bitOffset == w) {
0809:                        if (compression == 2) {
0810:                            advancePointer();
0811:                        }
0812:                        break;
0813:                    }
0814:
0815:                    while (!isWhite) {
0816:                        // Black run
0817:                        current = nextLesserThan8Bits(4);
0818:                        entry = initBlack[current];
0819:
0820:                        // Get the 3 fields from the entry
0821:                        isT = entry & 0x0001;
0822:                        bits = (entry >>> 1) & 0x000f;
0823:                        code = (entry >>> 5) & 0x07ff;
0824:
0825:                        if (code == 100) {
0826:                            current = nextNBits(9);
0827:                            entry = black[current];
0828:
0829:                            // Get the 3 fields from the entry
0830:                            isT = entry & 0x0001;
0831:                            bits = (entry >>> 1) & 0x000f;
0832:                            code = (entry >>> 5) & 0x07ff;
0833:
0834:                            if (bits == 12) {
0835:                                // Additional makeup codes
0836:                                updatePointer(5);
0837:                                current = nextLesserThan8Bits(4);
0838:                                entry = additionalMakeup[current];
0839:                                bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
0840:                                code = (entry >>> 4) & 0x0fff; // 12 bits
0841:
0842:                                setToBlack(buffer, lineOffset, bitOffset, code);
0843:                                bitOffset += code;
0844:
0845:                                updatePointer(4 - bits);
0846:                            } else if (bits == 15) {
0847:                                // EOL code
0848:                                throw new Error("TIFFFaxDecoder2");
0849:                            } else {
0850:                                setToBlack(buffer, lineOffset, bitOffset, code);
0851:                                bitOffset += code;
0852:
0853:                                updatePointer(9 - bits);
0854:                                if (isT == 0) {
0855:                                    isWhite = true;
0856:                                    currChangingElems[changingElemSize++] = bitOffset;
0857:                                }
0858:                            }
0859:                        } else if (code == 200) {
0860:                            // Is a Terminating code
0861:                            current = nextLesserThan8Bits(2);
0862:                            entry = twoBitBlack[current];
0863:                            code = (entry >>> 5) & 0x07ff;
0864:                            bits = (entry >>> 1) & 0x0f;
0865:
0866:                            setToBlack(buffer, lineOffset, bitOffset, code);
0867:                            bitOffset += code;
0868:
0869:                            updatePointer(2 - bits);
0870:                            isWhite = true;
0871:                            currChangingElems[changingElemSize++] = bitOffset;
0872:                        } else {
0873:                            // Is a Terminating code
0874:                            setToBlack(buffer, lineOffset, bitOffset, code);
0875:                            bitOffset += code;
0876:
0877:                            updatePointer(4 - bits);
0878:                            isWhite = true;
0879:                            currChangingElems[changingElemSize++] = bitOffset;
0880:                        }
0881:                    }
0882:
0883:                    // Check whether this run completed one width
0884:                    if (bitOffset == w) {
0885:                        if (compression == 2) {
0886:                            advancePointer();
0887:                        }
0888:                        break;
0889:                    }
0890:                }
0891:
0892:                currChangingElems[changingElemSize++] = bitOffset;
0893:            }
0894:
0895:            // Two-dimensional decoding methods
0896:
0897:            public void decode2D(byte[] buffer, byte[] compData, int startX,
0898:                    int height, long tiffT4Options) {
0899:                this .data = compData;
0900:                compression = 3;
0901:
0902:                bitPointer = 0;
0903:                bytePointer = 0;
0904:
0905:                int scanlineStride = (w + 7) / 8;
0906:
0907:                int a0, a1, b1, b2;
0908:                int[] b = new int[2];
0909:                int entry, code, bits;
0910:                boolean isWhite;
0911:                int currIndex = 0;
0912:                int[] temp;
0913:
0914:                // fillBits - dealt with this in readEOL
0915:                // 1D/2D encoding - dealt with this in readEOL
0916:
0917:                // uncompressedMode - haven't dealt with this yet.
0918:
0919:                oneD = (int) (tiffT4Options & 0x01);
0920:                uncompressedMode = (int) ((tiffT4Options & 0x02) >> 1);
0921:                fillBits = (int) ((tiffT4Options & 0x04) >> 2);
0922:
0923:                // The data must start with an EOL code
0924:                if (readEOL() != 1) {
0925:                    throw new Error("TIFFFaxDecoder3");
0926:                }
0927:
0928:                int lineOffset = 0;
0929:                int bitOffset;
0930:
0931:                // Then the 1D encoded scanline data will occur, changing elements
0932:                // array gets set.
0933:                decodeNextScanline(buffer, lineOffset, startX);
0934:                lineOffset += scanlineStride;
0935:
0936:                for (int lines = 1; lines < height; lines++) {
0937:
0938:                    // Every line must begin with an EOL followed by a bit which
0939:                    // indicates whether the following scanline is 1D or 2D encoded.
0940:                    if (readEOL() == 0) {
0941:                        // 2D encoded scanline follows
0942:
0943:                        // Initialize previous scanlines changing elements, and
0944:                        // initialize current scanline's changing elements array
0945:                        temp = prevChangingElems;
0946:                        prevChangingElems = currChangingElems;
0947:                        currChangingElems = temp;
0948:                        currIndex = 0;
0949:
0950:                        // a0 has to be set just before the start of this scanline.
0951:                        a0 = -1;
0952:                        isWhite = true;
0953:                        bitOffset = startX;
0954:
0955:                        lastChangingElement = 0;
0956:
0957:                        while (bitOffset < w) {
0958:                            // Get the next changing element
0959:                            getNextChangingElement(a0, isWhite, b);
0960:
0961:                            b1 = b[0];
0962:                            b2 = b[1];
0963:
0964:                            // Get the next seven bits
0965:                            entry = nextLesserThan8Bits(7);
0966:
0967:                            // Run these through the 2DCodes table
0968:                            entry = (int) (twoDCodes[entry] & 0xff);
0969:
0970:                            // Get the code and the number of bits used up
0971:                            code = (entry & 0x78) >>> 3;
0972:                            bits = entry & 0x07;
0973:
0974:                            if (code == 0) {
0975:                                if (!isWhite) {
0976:                                    setToBlack(buffer, lineOffset, bitOffset,
0977:                                            b2 - bitOffset);
0978:                                }
0979:                                bitOffset = a0 = b2;
0980:
0981:                                // Set pointer to consume the correct number of bits.
0982:                                updatePointer(7 - bits);
0983:                            } else if (code == 1) {
0984:                                // Horizontal
0985:                                updatePointer(7 - bits);
0986:
0987:                                // identify the next 2 codes.
0988:                                int number;
0989:                                if (isWhite) {
0990:                                    number = decodeWhiteCodeWord();
0991:                                    bitOffset += number;
0992:                                    currChangingElems[currIndex++] = bitOffset;
0993:
0994:                                    number = decodeBlackCodeWord();
0995:                                    setToBlack(buffer, lineOffset, bitOffset,
0996:                                            number);
0997:                                    bitOffset += number;
0998:                                    currChangingElems[currIndex++] = bitOffset;
0999:                                } else {
1000:                                    number = decodeBlackCodeWord();
1001:                                    setToBlack(buffer, lineOffset, bitOffset,
1002:                                            number);
1003:                                    bitOffset += number;
1004:                                    currChangingElems[currIndex++] = bitOffset;
1005:
1006:                                    number = decodeWhiteCodeWord();
1007:                                    bitOffset += number;
1008:                                    currChangingElems[currIndex++] = bitOffset;
1009:                                }
1010:
1011:                                a0 = bitOffset;
1012:                            } else if (code <= 8) {
1013:                                // Vertical
1014:                                a1 = b1 + (code - 5);
1015:
1016:                                currChangingElems[currIndex++] = a1;
1017:
1018:                                // We write the current color till a1 - 1 pos,
1019:                                // since a1 is where the next color starts
1020:                                if (!isWhite) {
1021:                                    setToBlack(buffer, lineOffset, bitOffset,
1022:                                            a1 - bitOffset);
1023:                                }
1024:                                bitOffset = a0 = a1;
1025:                                isWhite = !isWhite;
1026:
1027:                                updatePointer(7 - bits);
1028:                            } else {
1029:                                throw new Error("TIFFFaxDecoder4");
1030:                            }
1031:                        }
1032:
1033:                        // Add the changing element beyond the current scanline for the
1034:                        // other color too
1035:                        currChangingElems[currIndex++] = bitOffset;
1036:                        changingElemSize = currIndex;
1037:                    } else {
1038:                        // 1D encoded scanline follows
1039:                        decodeNextScanline(buffer, lineOffset, startX);
1040:                    }
1041:
1042:                    lineOffset += scanlineStride;
1043:                }
1044:            }
1045:
1046:            public synchronized void decodeT6(byte[] buffer, byte[] compData,
1047:                    int startX, int height, long tiffT6Options) {
1048:                this .data = compData;
1049:                compression = 4;
1050:
1051:                bitPointer = 0;
1052:                bytePointer = 0;
1053:
1054:                int scanlineStride = (w + 7) / 8;
1055:
1056:                int a0, a1, b1, b2;
1057:                int entry, code, bits;
1058:                boolean isWhite;
1059:                int currIndex;
1060:                int[] temp;
1061:
1062:                // Return values from getNextChangingElement
1063:                int[] b = new int[2];
1064:
1065:                // uncompressedMode - have written some code for this, but this
1066:                // has not been tested due to lack of test images using this optional
1067:
1068:                uncompressedMode = (int) ((tiffT6Options & 0x02) >> 1);
1069:
1070:                // Local cached reference
1071:                int[] cce = currChangingElems;
1072:
1073:                // Assume invisible preceding row of all white pixels and insert
1074:                // both black and white changing elements beyond the end of this
1075:                // imaginary scanline.
1076:                changingElemSize = 0;
1077:                cce[changingElemSize++] = w;
1078:                cce[changingElemSize++] = w;
1079:
1080:                int lineOffset = 0;
1081:                int bitOffset;
1082:
1083:                for (int lines = 0; lines < height; lines++) {
1084:                    // a0 has to be set just before the start of the scanline.
1085:                    a0 = -1;
1086:                    isWhite = true;
1087:
1088:                    // Assign the changing elements of the previous scanline to
1089:                    // prevChangingElems and start putting this new scanline's
1090:                    // changing elements into the currChangingElems.
1091:                    temp = prevChangingElems;
1092:                    prevChangingElems = currChangingElems;
1093:                    cce = currChangingElems = temp;
1094:                    currIndex = 0;
1095:
1096:                    // Start decoding the scanline at startX in the raster
1097:                    bitOffset = startX;
1098:
1099:                    // Reset search start position for getNextChangingElement
1100:                    lastChangingElement = 0;
1101:
1102:                    // Till one whole scanline is decoded
1103:                    while (bitOffset < w) {
1104:                        // Get the next changing element
1105:                        getNextChangingElement(a0, isWhite, b);
1106:                        b1 = b[0];
1107:                        b2 = b[1];
1108:
1109:                        // Get the next seven bits
1110:                        entry = nextLesserThan8Bits(7);
1111:                        // Run these through the 2DCodes table
1112:                        entry = (int) (twoDCodes[entry] & 0xff);
1113:
1114:                        // Get the code and the number of bits used up
1115:                        code = (entry & 0x78) >>> 3;
1116:                        bits = entry & 0x07;
1117:
1118:                        if (code == 0) { // Pass
1119:                            // We always assume WhiteIsZero format for fax.
1120:                            if (!isWhite) {
1121:                                setToBlack(buffer, lineOffset, bitOffset, b2
1122:                                        - bitOffset);
1123:                            }
1124:                            bitOffset = a0 = b2;
1125:
1126:                            // Set pointer to only consume the correct number of bits.
1127:                            updatePointer(7 - bits);
1128:                        } else if (code == 1) { // Horizontal
1129:                            // Set pointer to only consume the correct number of bits.
1130:                            updatePointer(7 - bits);
1131:
1132:                            // identify the next 2 alternating color codes.
1133:                            int number;
1134:                            if (isWhite) {
1135:                                // Following are white and black runs
1136:                                number = decodeWhiteCodeWord();
1137:                                bitOffset += number;
1138:                                cce[currIndex++] = bitOffset;
1139:
1140:                                number = decodeBlackCodeWord();
1141:                                setToBlack(buffer, lineOffset, bitOffset,
1142:                                        number);
1143:                                bitOffset += number;
1144:                                cce[currIndex++] = bitOffset;
1145:                            } else {
1146:                                // First a black run and then a white run follows
1147:                                number = decodeBlackCodeWord();
1148:                                setToBlack(buffer, lineOffset, bitOffset,
1149:                                        number);
1150:                                bitOffset += number;
1151:                                cce[currIndex++] = bitOffset;
1152:
1153:                                number = decodeWhiteCodeWord();
1154:                                bitOffset += number;
1155:                                cce[currIndex++] = bitOffset;
1156:                            }
1157:
1158:                            a0 = bitOffset;
1159:                        } else if (code <= 8) { // Vertical
1160:                            a1 = b1 + (code - 5);
1161:                            cce[currIndex++] = a1;
1162:
1163:                            // We write the current color till a1 - 1 pos,
1164:                            // since a1 is where the next color starts
1165:                            if (!isWhite) {
1166:                                setToBlack(buffer, lineOffset, bitOffset, a1
1167:                                        - bitOffset);
1168:                            }
1169:                            bitOffset = a0 = a1;
1170:                            isWhite = !isWhite;
1171:
1172:                            updatePointer(7 - bits);
1173:                        } else if (code == 11) {
1174:                            if (nextLesserThan8Bits(3) != 7) {
1175:                                throw new Error("TIFFFaxDecoder5");
1176:                            }
1177:
1178:                            int zeros = 0;
1179:                            boolean exit = false;
1180:
1181:                            while (!exit) {
1182:                                while (nextLesserThan8Bits(1) != 1) {
1183:                                    zeros++;
1184:                                }
1185:
1186:                                if (zeros > 5) {
1187:                                    // Exit code
1188:
1189:                                    // Zeros before exit code
1190:                                    zeros = zeros - 6;
1191:
1192:                                    if (!isWhite && (zeros > 0)) {
1193:                                        cce[currIndex++] = bitOffset;
1194:                                    }
1195:
1196:                                    // Zeros before the exit code
1197:                                    bitOffset += zeros;
1198:                                    if (zeros > 0) {
1199:                                        // Some zeros have been written
1200:                                        isWhite = true;
1201:                                    }
1202:
1203:                                    // Read in the bit which specifies the color of
1204:                                    // the following run
1205:                                    if (nextLesserThan8Bits(1) == 0) {
1206:                                        if (!isWhite) {
1207:                                            cce[currIndex++] = bitOffset;
1208:                                        }
1209:                                        isWhite = true;
1210:                                    } else {
1211:                                        if (isWhite) {
1212:                                            cce[currIndex++] = bitOffset;
1213:                                        }
1214:                                        isWhite = false;
1215:                                    }
1216:
1217:                                    exit = true;
1218:                                }
1219:
1220:                                if (zeros == 5) {
1221:                                    if (!isWhite) {
1222:                                        cce[currIndex++] = bitOffset;
1223:                                    }
1224:                                    bitOffset += zeros;
1225:
1226:                                    // Last thing written was white
1227:                                    isWhite = true;
1228:                                } else {
1229:                                    bitOffset += zeros;
1230:
1231:                                    cce[currIndex++] = bitOffset;
1232:                                    setToBlack(buffer, lineOffset, bitOffset, 1);
1233:                                    ++bitOffset;
1234:
1235:                                    // Last thing written was black
1236:                                    isWhite = false;
1237:                                }
1238:
1239:                            }
1240:                        } else {
1241:                            throw new Error("TIFFFaxDecoder5");
1242:                        }
1243:                    }
1244:
1245:                    // Add the changing element beyond the current scanline for the
1246:                    // other color too
1247:                    cce[currIndex++] = bitOffset;
1248:
1249:                    // Number of changing elements in this scanline.
1250:                    changingElemSize = currIndex;
1251:
1252:                    lineOffset += scanlineStride;
1253:                }
1254:            }
1255:
1256:            private void setToBlack(byte[] buffer, int lineOffset,
1257:                    int bitOffset, int numBits) {
1258:                int bitNum = 8 * lineOffset + bitOffset;
1259:                int lastBit = bitNum + numBits;
1260:
1261:                int byteNum = bitNum >> 3;
1262:
1263:                // Handle bits in first byte
1264:                int shift = bitNum & 0x7;
1265:                if (shift > 0) {
1266:                    int maskVal = 1 << (7 - shift);
1267:                    byte val = buffer[byteNum];
1268:                    while (maskVal > 0 && bitNum < lastBit) {
1269:                        val |= maskVal;
1270:                        maskVal >>= 1;
1271:                        ++bitNum;
1272:                    }
1273:                    buffer[byteNum] = val;
1274:                }
1275:
1276:                // Fill in 8 bits at a time
1277:                byteNum = bitNum >> 3;
1278:                while (bitNum < lastBit - 7) {
1279:                    buffer[byteNum++] = (byte) 255;
1280:                    bitNum += 8;
1281:                }
1282:
1283:                // Fill in remaining bits
1284:                while (bitNum < lastBit) {
1285:                    byteNum = bitNum >> 3;
1286:                    buffer[byteNum] |= 1 << (7 - (bitNum & 0x7));
1287:                    ++bitNum;
1288:                }
1289:            }
1290:
1291:            // Returns run length
1292:            private int decodeWhiteCodeWord() {
1293:                int current, entry, bits, isT, twoBits, code = -1;
1294:                int runLength = 0;
1295:                boolean isWhite = true;
1296:
1297:                while (isWhite) {
1298:                    current = nextNBits(10);
1299:                    entry = white[current];
1300:
1301:                    // Get the 3 fields from the entry
1302:                    isT = entry & 0x0001;
1303:                    bits = (entry >>> 1) & 0x0f;
1304:
1305:                    if (bits == 12) { // Additional Make up code
1306:                        // Get the next 2 bits
1307:                        twoBits = nextLesserThan8Bits(2);
1308:                        // Consolidate the 2 new bits and last 2 bits into 4 bits
1309:                        current = ((current << 2) & 0x000c) | twoBits;
1310:                        entry = additionalMakeup[current];
1311:                        bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
1312:                        code = (entry >>> 4) & 0x0fff; // 12 bits
1313:                        runLength += code;
1314:                        updatePointer(4 - bits);
1315:                    } else if (bits == 0) { // ERROR
1316:                        throw new Error("TIFFFaxDecoder0");
1317:                    } else if (bits == 15) { // EOL
1318:                        throw new Error("TIFFFaxDecoder1");
1319:                    } else {
1320:                        // 11 bits - 0000 0111 1111 1111 = 0x07ff
1321:                        code = (entry >>> 5) & 0x07ff;
1322:                        runLength += code;
1323:                        updatePointer(10 - bits);
1324:                        if (isT == 0) {
1325:                            isWhite = false;
1326:                        }
1327:                    }
1328:                }
1329:
1330:                return runLength;
1331:            }
1332:
1333:            // Returns run length
1334:            private int decodeBlackCodeWord() {
1335:                int current, entry, bits, isT, code = -1;
1336:                int runLength = 0;
1337:                boolean isWhite = false;
1338:
1339:                while (!isWhite) {
1340:                    current = nextLesserThan8Bits(4);
1341:                    entry = initBlack[current];
1342:
1343:                    // Get the 3 fields from the entry
1344:                    isT = entry & 0x0001;
1345:                    bits = (entry >>> 1) & 0x000f;
1346:                    code = (entry >>> 5) & 0x07ff;
1347:
1348:                    if (code == 100) {
1349:                        current = nextNBits(9);
1350:                        entry = black[current];
1351:
1352:                        // Get the 3 fields from the entry
1353:                        isT = entry & 0x0001;
1354:                        bits = (entry >>> 1) & 0x000f;
1355:                        code = (entry >>> 5) & 0x07ff;
1356:
1357:                        if (bits == 12) {
1358:                            // Additional makeup codes
1359:                            updatePointer(5);
1360:                            current = nextLesserThan8Bits(4);
1361:                            entry = additionalMakeup[current];
1362:                            bits = (entry >>> 1) & 0x07; // 3 bits 0000 0111
1363:                            code = (entry >>> 4) & 0x0fff; // 12 bits
1364:                            runLength += code;
1365:
1366:                            updatePointer(4 - bits);
1367:                        } else if (bits == 15) {
1368:                            // EOL code
1369:                            throw new Error("TIFFFaxDecoder2");
1370:                        } else {
1371:                            runLength += code;
1372:                            updatePointer(9 - bits);
1373:                            if (isT == 0) {
1374:                                isWhite = true;
1375:                            }
1376:                        }
1377:                    } else if (code == 200) {
1378:                        // Is a Terminating code
1379:                        current = nextLesserThan8Bits(2);
1380:                        entry = twoBitBlack[current];
1381:                        code = (entry >>> 5) & 0x07ff;
1382:                        runLength += code;
1383:                        bits = (entry >>> 1) & 0x0f;
1384:                        updatePointer(2 - bits);
1385:                        isWhite = true;
1386:                    } else {
1387:                        // Is a Terminating code
1388:                        runLength += code;
1389:                        updatePointer(4 - bits);
1390:                        isWhite = true;
1391:                    }
1392:                }
1393:
1394:                return runLength;
1395:            }
1396:
1397:            private int readEOL() {
1398:                if (fillBits == 0) {
1399:                    if (nextNBits(12) != 1) {
1400:                        throw new Error("TIFFFaxDecoder6");
1401:                    }
1402:                } else if (fillBits == 1) {
1403:
1404:                    // First EOL code word xxxx 0000 0000 0001 will occur
1405:                    // As many fill bits will be present as required to make
1406:                    // the EOL code of 12 bits end on a byte boundary.
1407:
1408:                    int bitsLeft = 8 - bitPointer;
1409:
1410:                    if (nextNBits(bitsLeft) != 0) {
1411:                        throw new Error("TIFFFaxDecoder8");
1412:                    }
1413:
1414:                    // If the number of bitsLeft is less than 8, then to have a 12
1415:                    // bit EOL sequence, two more bytes are certainly going to be
1416:                    // required. The first of them has to be all zeros, so ensure
1417:                    // that.
1418:                    if (bitsLeft < 4) {
1419:                        if (nextNBits(8) != 0) {
1420:                            throw new Error("TIFFFaxDecoder8");
1421:                        }
1422:                    }
1423:
1424:                    // There might be a random number of fill bytes with 0s, so
1425:                    // loop till the EOL of 0000 0001 is found, as long as all
1426:                    // the bytes preceding it are 0's.
1427:                    int n;
1428:                    while ((n = nextNBits(8)) != 1) {
1429:
1430:                        // If not all zeros
1431:                        if (n != 0) {
1432:                            throw new Error("TIFFFaxDecoder8");
1433:                        }
1434:                    }
1435:                }
1436:
1437:                // If one dimensional encoding mode, then always return 1
1438:                if (oneD == 0) {
1439:                    return 1;
1440:                } else {
1441:                    // Otherwise for 2D encoding mode,
1442:                    // The next one bit signifies 1D/2D encoding of next line.
1443:                    return nextLesserThan8Bits(1);
1444:                }
1445:            }
1446:
1447:            private void getNextChangingElement(int a0, boolean isWhite,
1448:                    int[] ret) {
1449:                // Local copies of instance variables
1450:                int[] pce = this .prevChangingElems;
1451:                int ces = this .changingElemSize;
1452:
1453:                // If the previous match was at an odd element, we still
1454:                // have to search the preceeding element.
1455:                // int start = lastChangingElement & ~0x1;
1456:                int start = lastChangingElement > 0 ? lastChangingElement - 1
1457:                        : 0;
1458:                if (isWhite) {
1459:                    start &= ~0x1; // Search even numbered elements
1460:                } else {
1461:                    start |= 0x1; // Search odd numbered elements
1462:                }
1463:
1464:                int i = start;
1465:                for (; i < ces; i += 2) {
1466:                    int temp = pce[i];
1467:                    if (temp > a0) {
1468:                        lastChangingElement = i;
1469:                        ret[0] = temp;
1470:                        break;
1471:                    }
1472:                }
1473:
1474:                if (i + 1 < ces) {
1475:                    ret[1] = pce[i + 1];
1476:                }
1477:            }
1478:
1479:            private int nextNBits(int bitsToGet) {
1480:                byte b, next, next2next;
1481:                int l = data.length - 1;
1482:                int bp = this .bytePointer;
1483:
1484:                if (fillOrder == 1) {
1485:                    b = data[bp];
1486:
1487:                    if (bp == l) {
1488:                        next = 0x00;
1489:                        next2next = 0x00;
1490:                    } else if ((bp + 1) == l) {
1491:                        next = data[bp + 1];
1492:                        next2next = 0x00;
1493:                    } else {
1494:                        next = data[bp + 1];
1495:                        next2next = data[bp + 2];
1496:                    }
1497:                } else if (fillOrder == 2) {
1498:                    b = flipTable[data[bp] & 0xff];
1499:
1500:                    if (bp == l) {
1501:                        next = 0x00;
1502:                        next2next = 0x00;
1503:                    } else if ((bp + 1) == l) {
1504:                        next = flipTable[data[bp + 1] & 0xff];
1505:                        next2next = 0x00;
1506:                    } else {
1507:                        next = flipTable[data[bp + 1] & 0xff];
1508:                        next2next = flipTable[data[bp + 2] & 0xff];
1509:                    }
1510:                } else {
1511:                    throw new Error("TIFFFaxDecoder7");
1512:                }
1513:
1514:                int bitsLeft = 8 - bitPointer;
1515:                int bitsFromNextByte = bitsToGet - bitsLeft;
1516:                int bitsFromNext2NextByte = 0;
1517:                if (bitsFromNextByte > 8) {
1518:                    bitsFromNext2NextByte = bitsFromNextByte - 8;
1519:                    bitsFromNextByte = 8;
1520:                }
1521:
1522:                bytePointer++;
1523:
1524:                int i1 = (b & table1[bitsLeft]) << (bitsToGet - bitsLeft);
1525:                int i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte);
1526:
1527:                int i3 = 0;
1528:                if (bitsFromNext2NextByte != 0) {
1529:                    i2 <<= bitsFromNext2NextByte;
1530:                    i3 = (next2next & table2[bitsFromNext2NextByte]) >>> (8 - bitsFromNext2NextByte);
1531:                    i2 |= i3;
1532:                    bytePointer++;
1533:                    bitPointer = bitsFromNext2NextByte;
1534:                } else {
1535:                    if (bitsFromNextByte == 8) {
1536:                        bitPointer = 0;
1537:                        bytePointer++;
1538:                    } else {
1539:                        bitPointer = bitsFromNextByte;
1540:                    }
1541:                }
1542:
1543:                int i = i1 | i2;
1544:                return i;
1545:            }
1546:
1547:            private int nextLesserThan8Bits(int bitsToGet) {
1548:                byte b, next;
1549:                int l = data.length - 1;
1550:                int bp = this .bytePointer;
1551:
1552:                if (fillOrder == 1) {
1553:                    b = data[bp];
1554:                    if (bp == l) {
1555:                        next = 0x00;
1556:                    } else {
1557:                        next = data[bp + 1];
1558:                    }
1559:                } else if (fillOrder == 2) {
1560:                    b = flipTable[data[bp] & 0xff];
1561:                    if (bp == l) {
1562:                        next = 0x00;
1563:                    } else {
1564:                        next = flipTable[data[bp + 1] & 0xff];
1565:                    }
1566:                } else {
1567:                    throw new Error("TIFFFaxDecoder7");
1568:                }
1569:
1570:                int bitsLeft = 8 - bitPointer;
1571:                int bitsFromNextByte = bitsToGet - bitsLeft;
1572:
1573:                int shift = bitsLeft - bitsToGet;
1574:                int i1, i2;
1575:                if (shift >= 0) {
1576:                    i1 = (b & table1[bitsLeft]) >>> shift;
1577:                    bitPointer += bitsToGet;
1578:                    if (bitPointer == 8) {
1579:                        bitPointer = 0;
1580:                        bytePointer++;
1581:                    }
1582:                } else {
1583:                    i1 = (b & table1[bitsLeft]) << (-shift);
1584:                    i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte);
1585:
1586:                    i1 |= i2;
1587:                    bytePointer++;
1588:                    bitPointer = bitsFromNextByte;
1589:                }
1590:
1591:                return i1;
1592:            }
1593:
1594:            // Move pointer backwards by given amount of bits
1595:            private void updatePointer(int bitsToMoveBack) {
1596:                int i = bitPointer - bitsToMoveBack;
1597:
1598:                if (i < 0) {
1599:                    bytePointer--;
1600:                    bitPointer = 8 + i;
1601:                } else {
1602:                    bitPointer = i;
1603:                }
1604:            }
1605:
1606:            // Move to the next byte boundary
1607:            private boolean advancePointer() {
1608:                if (bitPointer != 0) {
1609:                    bytePointer++;
1610:                    bitPointer = 0;
1611:                }
1612:
1613:                return true;
1614:            }
1615:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.