Source Code Cross Referenced for TIFFFaxDecoder.java in  » PDF » pdf-itext » com » lowagie » text » pdf » codec » 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 » PDF » pdf itext » com.lowagie.text.pdf.codec 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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