Source Code Cross Referenced for StdEntropyCoder.java in  » 6.0-JDK-Modules » Java-Advanced-Imaging » jj2000 » j2k » entropy » encoder » 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 » 6.0 JDK Modules » Java Advanced Imaging » jj2000.j2k.entropy.encoder 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * $RCSfile: StdEntropyCoder.java,v $
0003:         * $Revision: 1.3 $
0004:         * $Date: 2005/09/26 22:08:13 $
0005:         * $State: Exp $
0006:         *
0007:         * Class:                   StdEntropyCoder
0008:         *
0009:         * Description:             Entropy coding engine of stripes in code-blocks
0010:         *
0011:         *
0012:         *
0013:         * COPYRIGHT:
0014:         *
0015:         * This software module was originally developed by Raphaël Grosbois and
0016:         * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
0017:         * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
0018:         * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
0019:         * Centre France S.A) in the course of development of the JPEG2000
0020:         * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
0021:         * software module is an implementation of a part of the JPEG 2000
0022:         * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
0023:         * Systems AB and Canon Research Centre France S.A (collectively JJ2000
0024:         * Partners) agree not to assert against ISO/IEC and users of the JPEG
0025:         * 2000 Standard (Users) any of their rights under the copyright, not
0026:         * including other intellectual property rights, for this software module
0027:         * with respect to the usage by ISO/IEC and Users of this software module
0028:         * or modifications thereof for use in hardware or software products
0029:         * claiming conformance to the JPEG 2000 Standard. Those intending to use
0030:         * this software module in hardware or software products are advised that
0031:         * their use may infringe existing patents. The original developers of
0032:         * this software module, JJ2000 Partners and ISO/IEC assume no liability
0033:         * for use of this software module or modifications thereof. No license
0034:         * or right to this software module is granted for non JPEG 2000 Standard
0035:         * conforming products. JJ2000 Partners have full right to use this
0036:         * software module for his/her own purpose, assign or donate this
0037:         * software module to any third party and to inhibit third parties from
0038:         * using this software module for non JPEG 2000 Standard conforming
0039:         * products. This copyright notice must be included in all copies or
0040:         * derivative works of this software module.
0041:         *
0042:         * Copyright (c) 1999/2000 JJ2000 Partners.
0043:         * */
0044:        package jj2000.j2k.entropy.encoder;
0045:
0046:        import java.awt.Point;
0047:
0048:        import jj2000.j2k.quantization.quantizer.*;
0049:        import jj2000.j2k.wavelet.analysis.*;
0050:        import jj2000.j2k.codestream.*;
0051:        import jj2000.j2k.wavelet.*;
0052:        import jj2000.j2k.entropy.*;
0053:        import jj2000.j2k.image.*;
0054:        import jj2000.j2k.util.*;
0055:        import jj2000.j2k.io.*;
0056:        import jj2000.j2k.*;
0057:
0058:        import java.util.*;
0059:        import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava;
0060:
0061:        /**
0062:         * This class implements the JPEG 2000 entropy coder, which codes stripes in
0063:         * code-blocks. This entropy coding engine can function in a single-threaded
0064:         * mode where one code-block is encoded at a time, or in a multi-threaded mode
0065:         * where multiple code-blocks are entropy coded in parallel. The interface
0066:         * presented by this class is the same in both modes.
0067:         *
0068:         * <p>The number of threads used by this entropy coder is specified by the
0069:         * "jj2000.j2k.entropy.encoder.StdEntropyCoder.nthreads" Java system
0070:         * property. If set to "0" the single threaded implementation is used. If set
0071:         * to 'n' ('n' larger than 0) then 'n' extra threads are started by this class
0072:         * which are used to encode the code-blocks in parallel (i.e. ideally 'n'
0073:         * code-blocks will be encoded in parallel at a time). On multiprocessor
0074:         * machines under a "native threads" Java Virtual Machine implementation each
0075:         * one of these threads can run on a separate processor speeding up the
0076:         * encoding time. By default the single-threaded implementation is used. The
0077:         * multi-threaded implementation currently assumes that the vast majority of
0078:         * consecutive calls to 'getNextCodeBlock()' will be done on the same
0079:         * component. If this is not the case, the speed-up that can be expected on
0080:         * multiprocessor machines might be significantly decreased.
0081:         *
0082:         * <p>The code-blocks are rectangular, with dimensions which must be powers of
0083:         * 2. Each dimension has to be no smaller than 4 and no larger than 256. The
0084:         * product of the two dimensions (i.e. area of the code-block) may not exceed
0085:         * 4096.
0086:         *
0087:         * <p>Context 0 of the MQ-coder is used as the uniform one (uniform,
0088:         * non-adaptive probability distribution). Context 1 is used for RLC
0089:         * coding. Contexts 2-10 are used for zero-coding (ZC), contexts 11-15 are
0090:         * used for sign-coding (SC) and contexts 16-18 are used for
0091:         * magnitude-refinement (MR).
0092:         *
0093:         * <p>This implementation buffers the symbols and calls the MQ coder only once
0094:         * per stripe and per coding pass, to reduce the method call overhead.
0095:         *
0096:         * <p>This implementation also provides some timing features. They can be
0097:         * enabled by setting the 'DO_TIMING' constant of this class to true and
0098:         * recompiling. The timing uses the 'System.currentTimeMillis()' Java API
0099:         * call, which returns wall clock time, not the actual CPU time used. The
0100:         * timing results will be printed on the message output. Since the times
0101:         * reported are wall clock times and not CPU usage times they can not be added
0102:         * to find the total used time (i.e. some time might be counted in several
0103:         * places). When timing is disabled ('DO_TIMING' is false) there is no penalty
0104:         * if the compiler performs some basic optimizations. Even if not the penalty
0105:         * should be negligeable.
0106:         *
0107:         * <p>The source module must implement the CBlkQuantDataSrcEnc interface and
0108:         * code-block's data is received in a CBlkWTData instance. This modules sends
0109:         * code-block's information in a CBlkRateDistStats instance.
0110:         *
0111:         * @see CBlkQuantDataSrcEnc
0112:         * @see CBlkWTData
0113:         * @see CBlkRateDistStats
0114:         * */
0115:        public class StdEntropyCoder extends EntropyCoder implements 
0116:                StdEntropyCoderOptions {
0117:
0118:            /** Whether to collect timing information or not: false. Used as a compile
0119:             * time directive. */
0120:            private final static boolean DO_TIMING = false;
0121:
0122:            /** The cumulative wall time for the entropy coding engine, for each
0123:             * component. In the single-threaded implementation it is the total time,
0124:             * in the multi-threaded implementation it is the time spent managing the
0125:             * compressor threads only. */
0126:            private long time[];
0127:
0128:            /** The Java system property name for the number of threads to use:
0129:             jj2000.j2k.entropy.encoder.StdEntropyCoder.nthreads */
0130:            public static final String THREADS_PROP_NAME = "jj2000.j2k.entropy.encoder.StdEntropyCoder.nthreads";
0131:
0132:            /** The default value for the property in THREADS_PROP_NAME: 0 */
0133:            public static final String DEF_THREADS_NUM = "0";
0134:
0135:            /** The increase in priority for the compressor threads, currently 3. The
0136:             * compressor threads will have a priority of THREADS_PRIORITY_INC more
0137:             * than the priority of the thread calling this class constructor. Used
0138:             * only in the multi-threaded implementation. */
0139:            public static final int THREADS_PRIORITY_INC = 0;
0140:
0141:            /** The pool of threads, for the threaded implementation. It is null, if
0142:             * non threaded implementation is used */
0143:            private ThreadPool tPool;
0144:
0145:            /** The queue of idle compressors. Used in multithreaded
0146:                implementation only */
0147:            private Stack idleComps;
0148:
0149:            /** The queue of completed compressors, for each component. Used
0150:                in multithreaded implementation only. */
0151:            private Stack completedComps[];
0152:
0153:            /** The number of busy compressors, for each component. Used in
0154:                multithreaded implementation only. */
0155:            private int nBusyComps[];
0156:
0157:            /** A flag indicating for each component if all the code-blocks of the *
0158:                current tile have been returned. Used in multithreaded implementation
0159:                only. */
0160:            private boolean finishedTileComponent[];
0161:
0162:            /** The MQ coder used, for each thread */
0163:            private MQCoder mqT[];
0164:
0165:            /** The raw bit output used, for each thread */
0166:            private BitToByteOutput boutT[];
0167:
0168:            /** The output stream used, for each thread */
0169:            private ByteOutputBuffer outT[];
0170:
0171:            /** The code-block size specifications */
0172:            private CBlkSizeSpec cblks;
0173:
0174:            /** The precinct partition specifications */
0175:            private PrecinctSizeSpec pss;
0176:
0177:            /** By-pass mode specifications */
0178:            public StringSpec bms;
0179:
0180:            /** MQ reset specifications */
0181:            public StringSpec mqrs;
0182:
0183:            /** Regular termination specifications */
0184:            public StringSpec rts;
0185:
0186:            /** Causal stripes specifications */
0187:            public StringSpec css;
0188:
0189:            /** Error resilience segment symbol use specifications */
0190:            public StringSpec sss;
0191:
0192:            /** The length calculation specifications */
0193:            public StringSpec lcs;
0194:
0195:            /** The termination type specifications */
0196:            public StringSpec tts;
0197:
0198:            /** The options that are turned on, as flag bits. One element for
0199:             * each tile-component. The options are 'OPT_TERM_PASS',
0200:             * 'OPT_RESET_MQ', 'OPT_VERT_STR_CAUSAL', 'OPT_BYPASS' and
0201:             * 'OPT_SEG_SYMBOLS' as defined in the StdEntropyCoderOptions
0202:             * interface
0203:             *
0204:             * @see StdEntropyCoderOptions
0205:             * */
0206:            private int[][] opts = null;
0207:
0208:            /** The length calculation type for each tile-component */
0209:            private int[][] lenCalc = null;
0210:
0211:            /** The termination type for each tile-component */
0212:            private int[][] tType = null;
0213:
0214:            /** Number of bits used for the Zero Coding lookup table */
0215:            private static final int ZC_LUT_BITS = 8;
0216:
0217:            /** Zero Coding context lookup tables for the LH global orientation */
0218:            private static final int ZC_LUT_LH[] = new int[1 << ZC_LUT_BITS];
0219:
0220:            /** Zero Coding context lookup tables for the HL global orientation */
0221:            private static final int ZC_LUT_HL[] = new int[1 << ZC_LUT_BITS];
0222:
0223:            /** Zero Coding context lookup tables for the HH global orientation */
0224:            private static final int ZC_LUT_HH[] = new int[1 << ZC_LUT_BITS];
0225:
0226:            /** Number of bits used for the Sign Coding lookup table */
0227:            private static final int SC_LUT_BITS = 9;
0228:
0229:            /** Sign Coding context lookup table. The index into the table is a 9 bit
0230:             * index, which correspond the the value in the 'state' array shifted by
0231:             * 'SC_SHIFT'. Bits 8-5 are the signs of the horizontal-left,
0232:             * horizontal-right, vertical-up and vertical-down neighbors,
0233:             * respectively. Bit 4 is not used (0 or 1 makes no difference). Bits 3-0
0234:             * are the significance of the horizontal-left, horizontal-right,
0235:             * vertical-up and vertical-down neighbors, respectively. The least 4 bits
0236:             * of the value in the lookup table define the context number and the sign
0237:             * bit defines the "sign predictor". */
0238:            private static final int SC_LUT[] = new int[1 << SC_LUT_BITS];
0239:
0240:            /** The mask to obtain the context index from the 'SC_LUT' */
0241:            private static final int SC_LUT_MASK = (1 << 4) - 1;
0242:
0243:            /** The shift to obtain the sign predictor from the 'SC_LUT'. It must be
0244:             * an unsigned shift. */
0245:            private static final int SC_SPRED_SHIFT = 31;
0246:
0247:            /** The sign bit for int data */
0248:            private static final int INT_SIGN_BIT = 1 << 31;
0249:
0250:            /** The number of bits used for the Magnitude Refinement lookup table */
0251:            private static final int MR_LUT_BITS = 9;
0252:
0253:            /** Magnitude Refinement context lookup table */
0254:            private static final int MR_LUT[] = new int[1 << MR_LUT_BITS];
0255:
0256:            /** The number of contexts used */
0257:            private static final int NUM_CTXTS = 19;
0258:
0259:            /** The RLC context */
0260:            private static final int RLC_CTXT = 1;
0261:
0262:            /** The UNIFORM context (with a uniform probability distribution which
0263:             * does not adapt) */
0264:            private static final int UNIF_CTXT = 0;
0265:
0266:            /** The initial states for the MQ coder */
0267:            private static final int MQ_INIT[] = { 46, 3, 4, 0, 0, 0, 0, 0, 0,
0268:                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0269:
0270:            /** The 4 symbol segmentation marker (1010) */
0271:            private static final int SEG_SYMBOLS[] = { 1, 0, 1, 0 };
0272:
0273:            /** The 4 contexts for the segmentation marker (always the UNIFORM context,
0274:             * UNIF_CTXT) */
0275:            private static final int SEG_SYMB_CTXTS[] = { UNIF_CTXT, UNIF_CTXT,
0276:                    UNIF_CTXT, UNIF_CTXT };
0277:
0278:            /**
0279:             * The state array for each thread. Each element of the state array stores
0280:             * the state of two coefficients. The lower 16 bits store the state of a
0281:             * coefficient in row 'i' and column 'j', while the upper 16 bits store
0282:             * the state of a coefficient in row 'i+1' and column 'j'. The 'i' row is
0283:             * either the first or the third row of a stripe. This packing of the
0284:             * states into 32 bit words allows a faster scan of all coefficients on
0285:             * each coding pass and diminished the amount of data transferred. The
0286:             * size of the state array is increased by 1 on each side (top, bottom,
0287:             * left, right) to handle boundary conditions without any special logic.
0288:             *
0289:             * <P>The state of a coefficient is stored in the following way in the
0290:             * lower 16 bits, where bit 0 is the least significant bit. Bit 15 is the
0291:             * significance of a coefficient (0 if non-significant, 1 otherwise). Bit
0292:             * 14 is the visited state (i.e. if a coefficient has been coded in the
0293:             * significance propagation pass of the current bit-plane). Bit 13 is the
0294:             * "non zero-context" state (i.e. if one of the eight immediate neighbors
0295:             * is significant it is 1, otherwise is 0). Bits 12 to 9 store the sign of
0296:             * the already significant left, right, up and down neighbors (1 for
0297:             * negative, 0 for positive or not yet significant). Bit 8 indicates if
0298:             * the magnitude refinement has already been applied to the
0299:             * coefficient. Bits 7 to 4 store the significance of the left, right, up
0300:             * and down neighbors (1 for significant, 0 for non significant). Bits 3
0301:             * to 0 store the significance of the diagonal coefficients (up-left,
0302:             * up-right, down-left and down-right; 1 for significant, 0 for non
0303:             * significant).
0304:             *
0305:             * <P>The upper 16 bits the state is stored as in the lower 16 bits,
0306:             * but with the bits shifted up by 16.
0307:             *
0308:             * <P>The lower 16 bits are referred to as "row 1" ("R1") while the upper
0309:             * 16 bits are referred to as "row 2" ("R2").
0310:             * */
0311:            private int stateT[][];
0312:
0313:            /* The separation between the upper and lower bits in the state array: 16
0314:             * */
0315:            private static final int STATE_SEP = 16;
0316:
0317:            /** The flag bit for the significance in the state array, for row 1. */
0318:            private static final int STATE_SIG_R1 = 1 << 15;
0319:
0320:            /** The flag bit for the "visited" bit in the state array, for row 1. */
0321:            private static final int STATE_VISITED_R1 = 1 << 14;
0322:
0323:            /** The flag bit for the "not zero context" bit in the state array, for
0324:             * row 1. This bit is always the OR of bits STATE_H_L_R1, STATE_H_R_R1,
0325:             * STATE_V_U_R1, STATE_V_D_R1, STATE_D_UL_R1, STATE_D_UR_R1, STATE_D_DL_R1
0326:             * and STATE_D_DR_R1. */
0327:            private static final int STATE_NZ_CTXT_R1 = 1 << 13;
0328:
0329:            /** The flag bit for the horizontal-left sign in the state array, for row
0330:             * 1. This bit can only be set if the STATE_H_L_R1 is also set. */
0331:            private static final int STATE_H_L_SIGN_R1 = 1 << 12;
0332:
0333:            /** The flag bit for the horizontal-right sign in the state array, for
0334:             * row 1. This bit can only be set if the STATE_H_R_R1 is also set. */
0335:            private static final int STATE_H_R_SIGN_R1 = 1 << 11;
0336:
0337:            /** The flag bit for the vertical-up sign in the state array, for row
0338:             * 1. This bit can only be set if the STATE_V_U_R1 is also set. */
0339:            private static final int STATE_V_U_SIGN_R1 = 1 << 10;
0340:
0341:            /** The flag bit for the vertical-down sign in the state array, for row
0342:             * 1. This bit can only be set if the STATE_V_D_R1 is also set. */
0343:            private static final int STATE_V_D_SIGN_R1 = 1 << 9;
0344:
0345:            /** The flag bit for the previous MR primitive applied in the state array,
0346:                for row 1. */
0347:            private static final int STATE_PREV_MR_R1 = 1 << 8;
0348:
0349:            /** The flag bit for the horizontal-left significance in the state array,
0350:                for row 1. */
0351:            private static final int STATE_H_L_R1 = 1 << 7;
0352:
0353:            /** The flag bit for the horizontal-right significance in the state array,
0354:                for row 1. */
0355:            private static final int STATE_H_R_R1 = 1 << 6;
0356:
0357:            /** The flag bit for the vertical-up significance in the state array, for
0358:             row 1.  */
0359:            private static final int STATE_V_U_R1 = 1 << 5;
0360:
0361:            /** The flag bit for the vertical-down significance in the state array,
0362:             for row 1.  */
0363:            private static final int STATE_V_D_R1 = 1 << 4;
0364:
0365:            /** The flag bit for the diagonal up-left significance in the state array,
0366:                for row 1. */
0367:            private static final int STATE_D_UL_R1 = 1 << 3;
0368:
0369:            /** The flag bit for the diagonal up-right significance in the state
0370:                array, for row 1.*/
0371:            private static final int STATE_D_UR_R1 = 1 << 2;
0372:
0373:            /** The flag bit for the diagonal down-left significance in the state
0374:                array, for row 1. */
0375:            private static final int STATE_D_DL_R1 = 1 << 1;
0376:
0377:            /** The flag bit for the diagonal down-right significance in the state
0378:                array , for row 1.*/
0379:            private static final int STATE_D_DR_R1 = 1;
0380:
0381:            /** The flag bit for the significance in the state array, for row 2. */
0382:            private static final int STATE_SIG_R2 = STATE_SIG_R1 << STATE_SEP;
0383:
0384:            /** The flag bit for the "visited" bit in the state array, for row 2. */
0385:            private static final int STATE_VISITED_R2 = STATE_VISITED_R1 << STATE_SEP;
0386:
0387:            /** The flag bit for the "not zero context" bit in the state array, for
0388:             * row 2. This bit is always the OR of bits STATE_H_L_R2, STATE_H_R_R2,
0389:             * STATE_V_U_R2, STATE_V_D_R2, STATE_D_UL_R2, STATE_D_UR_R2, STATE_D_DL_R2
0390:             * and STATE_D_DR_R2. */
0391:            private static final int STATE_NZ_CTXT_R2 = STATE_NZ_CTXT_R1 << STATE_SEP;
0392:
0393:            /** The flag bit for the horizontal-left sign in the state array, for row
0394:             * 2. This bit can only be set if the STATE_H_L_R2 is also set. */
0395:            private static final int STATE_H_L_SIGN_R2 = STATE_H_L_SIGN_R1 << STATE_SEP;
0396:
0397:            /** The flag bit for the horizontal-right sign in the state array, for
0398:             * row 2. This bit can only be set if the STATE_H_R_R2 is also set. */
0399:            private static final int STATE_H_R_SIGN_R2 = STATE_H_R_SIGN_R1 << STATE_SEP;
0400:
0401:            /** The flag bit for the vertical-up sign in the state array, for row
0402:             * 2. This bit can only be set if the STATE_V_U_R2 is also set. */
0403:            private static final int STATE_V_U_SIGN_R2 = STATE_V_U_SIGN_R1 << STATE_SEP;
0404:
0405:            /** The flag bit for the vertical-down sign in the state array, for row
0406:             * 2. This bit can only be set if the STATE_V_D_R2 is also set. */
0407:            private static final int STATE_V_D_SIGN_R2 = STATE_V_D_SIGN_R1 << STATE_SEP;
0408:
0409:            /** The flag bit for the previous MR primitive applied in the state array,
0410:                for row 2. */
0411:            private static final int STATE_PREV_MR_R2 = STATE_PREV_MR_R1 << STATE_SEP;
0412:
0413:            /** The flag bit for the horizontal-left significance in the state array,
0414:                for row 2. */
0415:            private static final int STATE_H_L_R2 = STATE_H_L_R1 << STATE_SEP;
0416:
0417:            /** The flag bit for the horizontal-right significance in the state array,
0418:                for row 2. */
0419:            private static final int STATE_H_R_R2 = STATE_H_R_R1 << STATE_SEP;
0420:
0421:            /** The flag bit for the vertical-up significance in the state array, for
0422:             row 2.  */
0423:            private static final int STATE_V_U_R2 = STATE_V_U_R1 << STATE_SEP;
0424:
0425:            /** The flag bit for the vertical-down significance in the state array,
0426:             for row 2.  */
0427:            private static final int STATE_V_D_R2 = STATE_V_D_R1 << STATE_SEP;
0428:
0429:            /** The flag bit for the diagonal up-left significance in the state array,
0430:                for row 2. */
0431:            private static final int STATE_D_UL_R2 = STATE_D_UL_R1 << STATE_SEP;
0432:
0433:            /** The flag bit for the diagonal up-right significance in the state
0434:                array, for row 2.*/
0435:            private static final int STATE_D_UR_R2 = STATE_D_UR_R1 << STATE_SEP;
0436:
0437:            /** The flag bit for the diagonal down-left significance in the state
0438:                array, for row 2. */
0439:            private static final int STATE_D_DL_R2 = STATE_D_DL_R1 << STATE_SEP;
0440:
0441:            /** The flag bit for the diagonal down-right significance in the state
0442:                array , for row 2.*/
0443:            private static final int STATE_D_DR_R2 = STATE_D_DR_R1 << STATE_SEP;
0444:
0445:            /** The mask to isolate the significance bits for row 1 and 2 of the state
0446:             * array. */
0447:            private static final int SIG_MASK_R1R2 = STATE_SIG_R1
0448:                    | STATE_SIG_R2;
0449:
0450:            /** The mask to isolate the visited bits for row 1 and 2 of the state
0451:             * array. */
0452:            private static final int VSTD_MASK_R1R2 = STATE_VISITED_R1
0453:                    | STATE_VISITED_R2;
0454:
0455:            /** The mask to isolate the bits necessary to identify RLC coding state
0456:             * (significant, visited and non-zero context, for row 1 and 2). */
0457:            private static final int RLC_MASK_R1R2 = STATE_SIG_R1
0458:                    | STATE_SIG_R2 | STATE_VISITED_R1 | STATE_VISITED_R2
0459:                    | STATE_NZ_CTXT_R1 | STATE_NZ_CTXT_R2;
0460:
0461:            /** The mask to obtain the ZC_LUT index from the state information */
0462:            // This is needed because of the STATE_V_D_SIGN_R1, STATE_V_U_SIGN_R1,
0463:            // STATE_H_R_SIGN_R1, and STATE_H_L_SIGN_R1 bits.
0464:            private static final int ZC_MASK = (1 << 8) - 1;
0465:
0466:            /** The shift to obtain the SC index to 'SC_LUT' from the state
0467:             * information, for row 1. */
0468:            private static final int SC_SHIFT_R1 = 4;
0469:
0470:            /** The shift to obtain the SC index to 'SC_LUT' from the state
0471:             * information, for row 2. */
0472:            private static final int SC_SHIFT_R2 = SC_SHIFT_R1 + STATE_SEP;
0473:
0474:            /** The bit mask to isolate the state bits relative to the sign coding
0475:             * lookup table ('SC_LUT'). */
0476:            private static final int SC_MASK = (1 << SC_LUT_BITS) - 1;
0477:
0478:            /** The mask to obtain the MR index to 'MR_LUT' from the 'state'
0479:             * information. It is to be applied after the 'MR_SHIFT'. */
0480:            private static final int MR_MASK = (1 << MR_LUT_BITS) - 1;
0481:
0482:            /** The number of bits used to index in the 'fm' lookup table, 7. The 'fs'
0483:             * table is indexed with one less bit. */
0484:            private static final int MSE_LKP_BITS = 7;
0485:
0486:            /** The number of fractional bits used to store data in the 'fm' and 'fs'
0487:             * lookup tables. */
0488:            private static final int MSE_LKP_FRAC_BITS = 13;
0489:
0490:            /** Distortion estimation lookup table for bits coded using the sign-code
0491:             * (SC) primative, for lossy coding (i.e. normal). */
0492:            private static final int FS_LOSSY[] = new int[1 << (MSE_LKP_BITS - 1)];
0493:
0494:            /** Distortion estimation lookup table for bits coded using the
0495:             * magnitude-refinement (MR) primative, for lossy coding (i.e. normal) */
0496:            private static final int FM_LOSSY[] = new int[1 << MSE_LKP_BITS];
0497:
0498:            /** Distortion estimation lookup table for bits coded using the sign-code
0499:             * (SC) primative, for lossless coding and last bit-plane. This table is
0500:             * different from 'fs_lossy' since when doing lossless coding the residual
0501:             * distortion after the last bit-plane is coded is strictly 0. */
0502:            private static final int FS_LOSSLESS[] = new int[1 << (MSE_LKP_BITS - 1)];
0503:
0504:            /** Distortion estimation lookup table for bits coded using the
0505:             * magnitude-refinement (MR) primative, for lossless coding and last
0506:             * bit-plane. This table is different from 'fs_lossless' since when doing
0507:             * lossless coding the residual distortion after the last bit-plane is
0508:             * coded is strictly 0.*/
0509:            private static final int FM_LOSSLESS[] = new int[1 << MSE_LKP_BITS];
0510:
0511:            /** The buffer for distortion values (avoids reallocation for each
0512:                code-block), for each thread. */
0513:            private double distbufT[][];
0514:
0515:            /** The buffer for rate values (avoids reallocation for each
0516:                code-block), for each thread. */
0517:            private int ratebufT[][];
0518:
0519:            /** The buffer for indicating terminated passes (avoids reallocation for
0520:             * each code-block), for each thread. */
0521:            private boolean istermbufT[][];
0522:
0523:            /** The source code-block to entropy code (avoids reallocation for each
0524:                code-block), for each thread. */
0525:            private CBlkWTData srcblkT[];
0526:
0527:            /** Buffer for symbols to send to the MQ-coder, for each thread. Used to
0528:             * reduce the number of calls to the MQ coder. */
0529:            // NOTE: The symbol buffer has not prooved to be of any great improvement
0530:            // in encoding time, but it does not hurt. It's performance should be
0531:            // better studied under different JVMs.
0532:            private int symbufT[][];
0533:
0534:            /** Buffer for the contexts to use when sending buffered symbols to the
0535:             * MQ-coder, for each thread. Used to reduce the number of calls to the MQ
0536:             * coder. */
0537:            private int ctxtbufT[][];
0538:
0539:            /** boolean used to signal if the precinct partition is used for
0540:             *  each component and each tile.  */
0541:            private boolean precinctPartition[][];
0542:
0543:            /**
0544:             * Class that takes care of running the 'compressCodeBlock()' method with
0545:             * thread local arguments. Used only in multithreaded implementation.
0546:             * */
0547:            private class Compressor implements  Runnable {
0548:                /** The index of this compressor. Used to access thread local
0549:                 * variables */
0550:                private final int idx;
0551:
0552:                /** The object where to store the compressed code-block */
0553:                // Should be private, but some buggy JDK 1.1 compilers complain
0554:                CBlkRateDistStats ccb;
0555:
0556:                /** The component on which to compress */
0557:                // Should be private, but some buggy JDK 1.1 compilers complain
0558:                int c;
0559:
0560:                /** The options bitmask to use in compression */
0561:                // Should be private, but some buggy JDK 1.1 compilers complain
0562:                int options;
0563:
0564:                /** The reversible flag to use in compression */
0565:                // Should be private, but some buggy JDK 1.1 compilers complain
0566:                boolean rev;
0567:
0568:                /** The length calculation type to use in compression */
0569:                // Should be private, but some buggy JDK 1.1 compilers complain
0570:                int lcType;
0571:
0572:                /** The MQ termination type to use in compression */
0573:                // Should be private, but some buggy JDK 1.1 compilers complain
0574:                int tType;
0575:
0576:                /** The cumulative wall time for this compressor, for each
0577:                 * component. */
0578:                private long time[];
0579:
0580:                /**
0581:                 * Creates a new compressor object with the given index.
0582:                 *
0583:                 * @param idx The index of this compressor.
0584:                 * */
0585:                Compressor(int idx) {
0586:                    this .idx = idx;
0587:                    if (DO_TIMING)
0588:                        time = new long[src.getNumComps()];
0589:                }
0590:
0591:                /**
0592:                 * Calls the 'compressCodeBlock()' method with thread local
0593:                 * arguments. Once completed it adds itself to the 'completedComps[c]'
0594:                 * stack, where 'c' is the component for which this compressor is
0595:                 * running. This last step occurs even if exceptions are thrown by the
0596:                 * 'compressCodeBlock()' method.
0597:                 * */
0598:                public void run() {
0599:                    // Start the code-block compression
0600:                    try {
0601:                        long stime = 0L;
0602:                        if (DO_TIMING)
0603:                            stime = System.currentTimeMillis();
0604:                        compressCodeBlock(c, ccb, srcblkT[idx], mqT[idx],
0605:                                boutT[idx], outT[idx], stateT[idx],
0606:                                distbufT[idx], ratebufT[idx], istermbufT[idx],
0607:                                symbufT[idx], ctxtbufT[idx], options, rev,
0608:                                lcType, tType);
0609:                        if (DO_TIMING)
0610:                            time[c] += System.currentTimeMillis() - stime;
0611:                    } finally {
0612:                        // Join the queue of completed compression, even if exceptions
0613:                        // occurred.
0614:                        completedComps[c].push(this );
0615:                    }
0616:                }
0617:
0618:                /**
0619:                 * Returns the wall time spent by this compressor for component 'c'
0620:                 * since the last call to this method (or the creation of this
0621:                 * compressor if not yet called). If DO_TIMING is false 0 is returned.
0622:                 *
0623:                 * @return The wall time in milliseconds spent by this compressor
0624:                 * since the last call to this method.
0625:                 * */
0626:                synchronized long getTiming(int c) {
0627:                    if (DO_TIMING) {
0628:                        long t = time[c];
0629:                        time[c] = 0L;
0630:                        return t;
0631:                    } else {
0632:                        return 0L;
0633:                    }
0634:                }
0635:
0636:                /**
0637:                 * Returns the index of this compressor.
0638:                 *
0639:                 * @return The index of this compressor.
0640:                 * */
0641:                public int getIdx() {
0642:                    return idx;
0643:                }
0644:            }
0645:
0646:            /** Static initializer: initializes all the lookup tables. */
0647:            static {
0648:                int i, j;
0649:                double val, deltaMSE;
0650:                int inter_sc_lut[];
0651:                int ds, us, rs, ls;
0652:                int dsgn, usgn, rsgn, lsgn;
0653:                int h, v;
0654:
0655:                // Initialize the zero coding lookup tables
0656:
0657:                // LH
0658:
0659:                // - No neighbors significant
0660:                ZC_LUT_LH[0] = 2;
0661:
0662:                // - No horizontal or vertical neighbors significant
0663:                for (i = 1; i < 16; i++) { // Two or more diagonal coeffs significant
0664:                    ZC_LUT_LH[i] = 4;
0665:                }
0666:                for (i = 0; i < 4; i++) { // Only one diagonal coeff significant
0667:                    ZC_LUT_LH[1 << i] = 3;
0668:                }
0669:                // - No horizontal neighbors significant, diagonal irrelevant
0670:                for (i = 0; i < 16; i++) {
0671:                    // Only one vertical coeff significant
0672:                    ZC_LUT_LH[STATE_V_U_R1 | i] = 5;
0673:                    ZC_LUT_LH[STATE_V_D_R1 | i] = 5;
0674:                    // The two vertical coeffs significant
0675:                    ZC_LUT_LH[STATE_V_U_R1 | STATE_V_D_R1 | i] = 6;
0676:                }
0677:                // - One horiz. neighbor significant, diagonal/vertical non-significant
0678:                ZC_LUT_LH[STATE_H_L_R1] = 7;
0679:                ZC_LUT_LH[STATE_H_R_R1] = 7;
0680:                // - One horiz. significant, no vertical significant, one or more
0681:                // diagonal significant
0682:                for (i = 1; i < 16; i++) {
0683:                    ZC_LUT_LH[STATE_H_L_R1 | i] = 8;
0684:                    ZC_LUT_LH[STATE_H_R_R1 | i] = 8;
0685:                }
0686:                // - One horiz. significant, one or more vertical significant,
0687:                // diagonal irrelevant
0688:                for (i = 1; i < 4; i++) {
0689:                    for (j = 0; j < 16; j++) {
0690:                        ZC_LUT_LH[STATE_H_L_R1 | (i << 4) | j] = 9;
0691:                        ZC_LUT_LH[STATE_H_R_R1 | (i << 4) | j] = 9;
0692:                    }
0693:                }
0694:                // - Two horiz. significant, others irrelevant
0695:                for (i = 0; i < 64; i++) {
0696:                    ZC_LUT_LH[STATE_H_L_R1 | STATE_H_R_R1 | i] = 10;
0697:                }
0698:
0699:                // HL
0700:
0701:                // - No neighbors significant
0702:                ZC_LUT_HL[0] = 2;
0703:                // - No horizontal or vertical neighbors significant
0704:                for (i = 1; i < 16; i++) { // Two or more diagonal coeffs significant
0705:                    ZC_LUT_HL[i] = 4;
0706:                }
0707:                for (i = 0; i < 4; i++) { // Only one diagonal coeff significant
0708:                    ZC_LUT_HL[1 << i] = 3;
0709:                }
0710:                // - No vertical significant, diagonal irrelevant
0711:                for (i = 0; i < 16; i++) {
0712:                    // One horiz. significant
0713:                    ZC_LUT_HL[STATE_H_L_R1 | i] = 5;
0714:                    ZC_LUT_HL[STATE_H_R_R1 | i] = 5;
0715:                    // Two horiz. significant
0716:                    ZC_LUT_HL[STATE_H_L_R1 | STATE_H_R_R1 | i] = 6;
0717:                }
0718:                // - One vert. significant, diagonal/horizontal non-significant
0719:                ZC_LUT_HL[STATE_V_U_R1] = 7;
0720:                ZC_LUT_HL[STATE_V_D_R1] = 7;
0721:                // - One vert. significant, horizontal non-significant, one or more
0722:                // diag. significant
0723:                for (i = 1; i < 16; i++) {
0724:                    ZC_LUT_HL[STATE_V_U_R1 | i] = 8;
0725:                    ZC_LUT_HL[STATE_V_D_R1 | i] = 8;
0726:                }
0727:                // - One vertical significant, one or more horizontal significant,
0728:                // diagonal irrelevant
0729:                for (i = 1; i < 4; i++) {
0730:                    for (j = 0; j < 16; j++) {
0731:                        ZC_LUT_HL[(i << 6) | STATE_V_U_R1 | j] = 9;
0732:                        ZC_LUT_HL[(i << 6) | STATE_V_D_R1 | j] = 9;
0733:                    }
0734:                }
0735:                // - Two vertical significant, others irrelevant
0736:                for (i = 0; i < 4; i++) {
0737:                    for (j = 0; j < 16; j++) {
0738:                        ZC_LUT_HL[(i << 6) | STATE_V_U_R1 | STATE_V_D_R1 | j] = 10;
0739:                    }
0740:                }
0741:
0742:                // HH
0743:                int[] twoBits = { 3, 5, 6, 9, 10, 12 }; // Figures (between 0 and 15)
0744:                // countaning 2 and only 2 bits on in its binary representation.
0745:
0746:                int[] oneBit = { 1, 2, 4, 8 }; // Figures (between 0 and 15)
0747:                // countaning 1 and only 1 bit on in its binary representation.
0748:
0749:                int[] twoLeast = { 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15 }; // Figures
0750:                // (between 0 and 15) countaining, at least, 2 bits on in its
0751:                // binary representation.
0752:
0753:                int[] threeLeast = { 7, 11, 13, 14, 15 }; // Figures
0754:                // (between 0 and 15) countaining, at least, 3 bits on in its
0755:                // binary representation.
0756:
0757:                // - None significant
0758:                ZC_LUT_HH[0] = 2;
0759:
0760:                // - One horizontal+vertical significant, none diagonal
0761:                for (i = 0; i < oneBit.length; i++)
0762:                    ZC_LUT_HH[oneBit[i] << 4] = 3;
0763:
0764:                // - Two or more horizontal+vertical significant, diagonal non-signif
0765:                for (i = 0; i < twoLeast.length; i++)
0766:                    ZC_LUT_HH[twoLeast[i] << 4] = 4;
0767:
0768:                // - One diagonal significant, horiz./vert. non-significant
0769:                for (i = 0; i < oneBit.length; i++)
0770:                    ZC_LUT_HH[oneBit[i]] = 5;
0771:
0772:                // - One diagonal significant, one horiz.+vert. significant
0773:                for (i = 0; i < oneBit.length; i++)
0774:                    for (j = 0; j < oneBit.length; j++)
0775:                        ZC_LUT_HH[(oneBit[i] << 4) | oneBit[j]] = 6;
0776:
0777:                // - One diag signif, two or more horiz+vert signif
0778:                for (i = 0; i < twoLeast.length; i++)
0779:                    for (j = 0; j < oneBit.length; j++)
0780:                        ZC_LUT_HH[(twoLeast[i] << 4) | oneBit[j]] = 7;
0781:
0782:                // - Two diagonal significant, none horiz+vert significant
0783:                for (i = 0; i < twoBits.length; i++)
0784:                    ZC_LUT_HH[twoBits[i]] = 8;
0785:
0786:                // - Two diagonal significant, one or more horiz+vert significant
0787:                for (j = 0; j < twoBits.length; j++)
0788:                    for (i = 1; i < 16; i++)
0789:                        ZC_LUT_HH[(i << 4) | twoBits[j]] = 9;
0790:
0791:                // - Three or more diagonal significant, horiz+vert irrelevant
0792:                for (i = 0; i < 16; i++)
0793:                    for (j = 0; j < threeLeast.length; j++)
0794:                        ZC_LUT_HH[(i << 4) | threeLeast[j]] = 10;
0795:
0796:                // Initialize the SC lookup tables
0797:
0798:                // Use an intermediate sign code lookup table that is similar to the
0799:                // one in the VM text, in that it depends on the 'h' and 'v'
0800:                // quantities. The index into this table is a 6 bit index, the top 3
0801:                // bits are (h+1) and the low 3 bits (v+1).
0802:                inter_sc_lut = new int[36];
0803:                inter_sc_lut[(2 << 3) | 2] = 15;
0804:                inter_sc_lut[(2 << 3) | 1] = 14;
0805:                inter_sc_lut[(2 << 3) | 0] = 13;
0806:                inter_sc_lut[(1 << 3) | 2] = 12;
0807:                inter_sc_lut[(1 << 3) | 1] = 11;
0808:                inter_sc_lut[(1 << 3) | 0] = 12 | INT_SIGN_BIT;
0809:                inter_sc_lut[(0 << 3) | 2] = 13 | INT_SIGN_BIT;
0810:                inter_sc_lut[(0 << 3) | 1] = 14 | INT_SIGN_BIT;
0811:                inter_sc_lut[(0 << 3) | 0] = 15 | INT_SIGN_BIT;
0812:
0813:                // Using the intermediate sign code lookup table create the final
0814:                // one. The index into this table is a 9 bit index, the low 4 bits are
0815:                // the significance of the 4 horizontal/vertical neighbors, while the
0816:                // top 4 bits are the signs of those neighbors. The bit in the middle
0817:                // is ignored. This index arrangement matches the state bits in the
0818:                // 'state' array, thus direct addressing of the table can be done from
0819:                // the sate information.
0820:                for (i = 0; i < (1 << SC_LUT_BITS) - 1; i++) {
0821:                    ds = i & 0x01; // significance of down neighbor
0822:                    us = (i >> 1) & 0x01; // significance of up neighbor
0823:                    rs = (i >> 2) & 0x01; // significance of right neighbor
0824:                    ls = (i >> 3) & 0x01; // significance of left neighbor
0825:                    dsgn = (i >> 5) & 0x01; // sign of down neighbor
0826:                    usgn = (i >> 6) & 0x01; // sign of up neighbor
0827:                    rsgn = (i >> 7) & 0x01; // sign of right neighbor
0828:                    lsgn = (i >> 8) & 0x01; // sign of left neighbor
0829:                    // Calculate 'h' and 'v' as in VM text
0830:                    h = ls * (1 - 2 * lsgn) + rs * (1 - 2 * rsgn);
0831:                    h = (h >= -1) ? h : -1;
0832:                    h = (h <= 1) ? h : 1;
0833:                    v = us * (1 - 2 * usgn) + ds * (1 - 2 * dsgn);
0834:                    v = (v >= -1) ? v : -1;
0835:                    v = (v <= 1) ? v : 1;
0836:                    // Get context and sign predictor from 'inter_sc_lut'
0837:                    SC_LUT[i] = inter_sc_lut[(h + 1) << 3 | (v + 1)];
0838:                }
0839:                inter_sc_lut = null;
0840:
0841:                // Initialize the MR lookup tables
0842:
0843:                // None significant, prev MR off
0844:                MR_LUT[0] = 16;
0845:                // One or more significant, prev MR off
0846:                for (i = 1; i < (1 << (MR_LUT_BITS - 1)); i++) {
0847:                    MR_LUT[i] = 17;
0848:                }
0849:                // Previous MR on, significance irrelevant
0850:                for (; i < (1 << MR_LUT_BITS); i++) {
0851:                    MR_LUT[i] = 18;
0852:                }
0853:
0854:                // Initialize the distortion estimation lookup tables
0855:
0856:                // fs tables
0857:                for (i = 0; i < (1 << (MSE_LKP_BITS - 1)); i++) {
0858:                    // In fs we index by val-1, since val is really: 1 <= val < 2
0859:                    val = (double) i / (1 << (MSE_LKP_BITS - 1)) + 1.0;
0860:                    deltaMSE = val * val;
0861:                    FS_LOSSLESS[i] = (int) Math.floor(deltaMSE
0862:                            * ((double) (1 << MSE_LKP_FRAC_BITS)) + 0.5);
0863:                    val -= 1.5;
0864:                    deltaMSE -= val * val;
0865:                    FS_LOSSY[i] = (int) Math.floor(deltaMSE
0866:                            * ((double) (1 << MSE_LKP_FRAC_BITS)) + 0.5);
0867:                }
0868:
0869:                // fm tables
0870:                for (i = 0; i < (1 << MSE_LKP_BITS); i++) {
0871:                    val = (double) i / (1 << (MSE_LKP_BITS - 1));
0872:                    deltaMSE = (val - 1.0) * (val - 1.0);
0873:                    FM_LOSSLESS[i] = (int) Math.floor(deltaMSE
0874:                            * ((double) (1 << MSE_LKP_FRAC_BITS)) + 0.5);
0875:                    val -= (i < (1 << (MSE_LKP_BITS - 1))) ? 0.5 : 1.5;
0876:                    deltaMSE -= val * val;
0877:                    FM_LOSSY[i] = (int) Math.floor(deltaMSE
0878:                            * ((double) (1 << MSE_LKP_FRAC_BITS)) + 0.5);
0879:                }
0880:            }
0881:
0882:            /**
0883:             * Instantiates a new entropy coder engine, with the specified source of
0884:             * data, nominal block width and height.
0885:             *
0886:             * <p>If the 'OPT_PRED_TERM' option is given then the MQ termination must
0887:             * be 'TERM_PRED_ER' or an exception is thrown.</p>
0888:             *
0889:             * @param src The source of data
0890:             *
0891:             * @param cbks Code-block size specifications
0892:             *
0893:             * @param pss Precinct partition specifications
0894:             *
0895:             * @param bms By-pass mode specifications
0896:             *
0897:             * @param mqrs MQ-reset specifications
0898:             *
0899:             * @param rts Regular termination specifications
0900:             *
0901:             * @param css Causal stripes specifications
0902:             *
0903:             * @param sss Error resolution segment symbol use specifications
0904:             *
0905:             * @param lcs Length computation specifications
0906:             *
0907:             * @param tts Termination type specifications
0908:             *
0909:             * @see MQCoder
0910:             * */
0911:            public StdEntropyCoder(CBlkQuantDataSrcEnc src, CBlkSizeSpec cblks,
0912:                    PrecinctSizeSpec pss, StringSpec bms, StringSpec mqrs,
0913:                    StringSpec rts, StringSpec css, StringSpec sss,
0914:                    StringSpec lcs, StringSpec tts) {
0915:                super (src);
0916:                this .cblks = cblks;
0917:                this .pss = pss;
0918:                this .bms = bms;
0919:                this .mqrs = mqrs;
0920:                this .rts = rts;
0921:                this .css = css;
0922:                this .sss = sss;
0923:                this .lcs = lcs;
0924:                this .tts = tts;
0925:                int maxCBlkWidth, maxCBlkHeight;
0926:                int i; // Counter
0927:                int nt; // The number of threads
0928:                int tsl; // Size for thread structures
0929:
0930:                // Get the biggest width/height for the code-blocks
0931:                maxCBlkWidth = cblks.getMaxCBlkWidth();
0932:                maxCBlkHeight = cblks.getMaxCBlkHeight();
0933:
0934:                // Get the number of threads to use, or default to one
0935:                try {
0936:                    try {
0937:                        nt = Integer.parseInt(System.getProperty(
0938:                                THREADS_PROP_NAME, DEF_THREADS_NUM));
0939:                    } catch (SecurityException se) {
0940:                        // Use the default value.
0941:                        nt = Integer.parseInt(DEF_THREADS_NUM);
0942:                    }
0943:                    if (nt < 0)
0944:                        throw new NumberFormatException();
0945:                } catch (NumberFormatException e) {
0946:                    throw new IllegalArgumentException(
0947:                            "Invalid number of threads " + "for "
0948:                                    + "entropy coding in property "
0949:                                    + THREADS_PROP_NAME);
0950:                }
0951:
0952:                // If we do timing create necessary structures
0953:                if (DO_TIMING) {
0954:                    time = new long[src.getNumComps()];
0955:                    // If we are timing make sure that 'finalize' gets called.
0956:                    System.runFinalizersOnExit(true);
0957:                }
0958:
0959:                // If using multithreaded implementation get necessasry objects
0960:                if (nt > 0) {
0961:                    FacilityManager.getMsgLogger().printmsg(
0962:                            MsgLogger.INFO,
0963:                            "Using multithreaded entropy coder " + "with " + nt
0964:                                    + " compressor threads.");
0965:                    tsl = nt;
0966:                    tPool = new ThreadPool(nt, Thread.currentThread()
0967:                            .getPriority()
0968:                            + THREADS_PRIORITY_INC, "StdEntropyCoder");
0969:                    idleComps = new Stack();
0970:                    completedComps = new Stack[src.getNumComps()];
0971:                    nBusyComps = new int[src.getNumComps()];
0972:                    finishedTileComponent = new boolean[src.getNumComps()];
0973:                    for (i = src.getNumComps() - 1; i >= 0; i--) {
0974:                        completedComps[i] = new Stack();
0975:                    }
0976:                    for (i = 0; i < nt; i++) {
0977:                        idleComps.push(new StdEntropyCoder.Compressor(i));
0978:                    }
0979:                } else {
0980:                    tsl = 1;
0981:                    tPool = null;
0982:                    idleComps = null;
0983:                    completedComps = null;
0984:                    nBusyComps = null;
0985:                    finishedTileComponent = null;
0986:                }
0987:
0988:                // Allocate data structures
0989:                outT = new ByteOutputBuffer[tsl];
0990:                mqT = new MQCoder[tsl];
0991:                boutT = new BitToByteOutput[tsl];
0992:                stateT = new int[tsl][(maxCBlkWidth + 2)
0993:                        * ((maxCBlkHeight + 1) / 2 + 2)];
0994:                symbufT = new int[tsl][maxCBlkWidth * (STRIPE_HEIGHT * 2 + 2)];
0995:                ctxtbufT = new int[tsl][maxCBlkWidth * (STRIPE_HEIGHT * 2 + 2)];
0996:                distbufT = new double[tsl][32 * NUM_PASSES];
0997:                ratebufT = new int[tsl][32 * NUM_PASSES];
0998:                istermbufT = new boolean[tsl][32 * NUM_PASSES];
0999:                srcblkT = new CBlkWTData[tsl];
1000:                for (i = 0; i < tsl; i++) {
1001:                    outT[i] = new ByteOutputBuffer();
1002:                    mqT[i] = new MQCoder(outT[i], NUM_CTXTS, MQ_INIT);
1003:                }
1004:                precinctPartition = new boolean[src.getNumComps()][src
1005:                        .getNumTiles()];
1006:
1007:                // Create the subband description for each component and each tile
1008:                Point numTiles = src.getNumTiles(null);
1009:                //Subband sb = null;
1010:                int nc = getNumComps();
1011:                initTileComp(getNumTiles(), nc);
1012:
1013:                for (int c = 0; c < nc; c++) {
1014:                    for (int tY = 0; tY < numTiles.y; tY++) {
1015:                        for (int tX = 0; tX < numTiles.x; tX++) {
1016:                            precinctPartition[c][tIdx] = false;
1017:                        }
1018:                    }
1019:                }
1020:            }
1021:
1022:            /**
1023:             * Prints the timing information, if collected, and calls 'finalize' on
1024:             * the super class.
1025:             * */
1026:            public void finalize() throws Throwable {
1027:                if (DO_TIMING) {
1028:                    int c;
1029:                    StringBuffer sb;
1030:
1031:                    if (tPool == null) { // Single threaded implementation
1032:                        sb = new StringBuffer(
1033:                                "StdEntropyCoder compression wall "
1034:                                        + "clock time:");
1035:                        for (c = 0; c < time.length; c++) {
1036:                            sb.append("\n  component ");
1037:                            sb.append(c);
1038:                            sb.append(": ");
1039:                            sb.append(time[c]);
1040:                            sb.append(" ms");
1041:                        }
1042:                        FacilityManager.getMsgLogger().printmsg(MsgLogger.INFO,
1043:                                sb.toString());
1044:                    } else { // Multithreaded implementation
1045:                        Compressor compr;
1046:                        MsgLogger msglog = FacilityManager.getMsgLogger();
1047:
1048:                        sb = new StringBuffer("StdEntropyCoder manager thread "
1049:                                + "wall clock time:");
1050:                        for (c = 0; c < time.length; c++) {
1051:                            sb.append("\n  component ");
1052:                            sb.append(c);
1053:                            sb.append(": ");
1054:                            sb.append(time[c]);
1055:                            sb.append(" ms");
1056:                        }
1057:                        Enumeration enumVar = idleComps.elements();
1058:                        sb
1059:                                .append("\nStdEntropyCoder compressor threads wall clock "
1060:                                        + "time:");
1061:                        while (enumVar.hasMoreElements()) {
1062:                            compr = (Compressor) (enumVar.nextElement());
1063:                            for (c = 0; c < time.length; c++) {
1064:                                sb.append("\n  compressor ");
1065:                                sb.append(compr.getIdx());
1066:                                sb.append(", component ");
1067:                                sb.append(c);
1068:                                sb.append(": ");
1069:                                sb.append(compr.getTiming(c));
1070:                                sb.append(" ms");
1071:                            }
1072:                        }
1073:                        FacilityManager.getMsgLogger().printmsg(MsgLogger.INFO,
1074:                                sb.toString());
1075:                    }
1076:                }
1077:                super .finalize();
1078:            }
1079:
1080:            /**
1081:             * Returns the code-block width for the specified tile and component.
1082:             *
1083:             * @param t The tile index
1084:             * 
1085:             * @param c the component index
1086:             *
1087:             * @return The code-block width for the specified tile and component
1088:             * */
1089:            public int getCBlkWidth(int t, int c) {
1090:                return cblks.getCBlkWidth(ModuleSpec.SPEC_TILE_COMP, t, c);
1091:            }
1092:
1093:            /**
1094:             * Returns the code-block height for the specified tile and component.
1095:             *
1096:             * @param t The tile index
1097:             *
1098:             * @param c The component index
1099:             *
1100:             * @return The code-block height for the specified tile and component.
1101:             * */
1102:            public int getCBlkHeight(int t, int c) {
1103:                return cblks.getCBlkHeight(ModuleSpec.SPEC_TILE_COMP, t, c);
1104:            }
1105:
1106:            /**
1107:             * Returns the next coded code-block in the current tile for the specified
1108:             * component, as a copy (see below). The order in which code-blocks are
1109:             * returned is not specified. However each code-block is returned only
1110:             * once and all code-blocks will be returned if the method is called 'N'
1111:             * times, where 'N' is the number of code-blocks in the tile. After all
1112:             * the code-blocks have been returned for the current tile calls to this
1113:             * method will return 'null'.
1114:             *
1115:             * <P>When changing the current tile (through 'setTile()' or 'nextTile()')
1116:             * this method will always return the first code-block, as if this method
1117:             * was never called before for the new current tile.
1118:             *
1119:             * <P>The data returned by this method is always a copy of the internal
1120:             * data of this object, if any, and it can be modified "in place" without
1121:             * any problems after being returned.
1122:             *
1123:             * @param c The component for which to return the next code-block.
1124:             *
1125:             * @param ccb If non-null this object might be used in returning the coded
1126:             * code-block in this or any subsequent call to this method. If null a new
1127:             * one is created and returned. If the 'data' array of 'cbb' is not null
1128:             * it may be reused to return the compressed data.
1129:             *
1130:             * @return The next coded code-block in the current tile for component
1131:             * 'n', or null if all code-blocks for the current tile have been
1132:             * returned.
1133:             *
1134:             * @see CBlkRateDistStats
1135:             * */
1136:            public CBlkRateDistStats getNextCodeBlock(int c,
1137:                    CBlkRateDistStats ccb) {
1138:                long stime = 0L; // Start time for timed sections
1139:                if (tPool == null) { // Use single threaded implementation
1140:                    // Get code-block data from source
1141:                    srcblkT[0] = src.getNextInternCodeBlock(c, srcblkT[0]);
1142:
1143:                    if (DO_TIMING)
1144:                        stime = System.currentTimeMillis();
1145:                    if (srcblkT[0] == null) { // We got all code-blocks
1146:                        return null;
1147:                    }
1148:                    // Initialize thread local variables
1149:                    if ((opts[tIdx][c] & OPT_BYPASS) != 0 && boutT[0] == null) {
1150:                        boutT[0] = new BitToByteOutput(outT[0]);
1151:                    }
1152:                    // Initialize output code-block
1153:                    if (ccb == null) {
1154:                        ccb = new CBlkRateDistStats();
1155:                    }
1156:                    // Compress code-block
1157:                    compressCodeBlock(c, ccb, srcblkT[0], mqT[0], boutT[0],
1158:                            outT[0], stateT[0], distbufT[0], ratebufT[0],
1159:                            istermbufT[0], symbufT[0], ctxtbufT[0],
1160:                            opts[tIdx][c], isReversible(tIdx, c),
1161:                            lenCalc[tIdx][c], tType[tIdx][c]);
1162:                    if (DO_TIMING)
1163:                        time[c] += System.currentTimeMillis() - stime;
1164:                    // Return result
1165:                    return ccb;
1166:                } else { // Use multiple threaded implementation
1167:                    int cIdx; // Compressor idx
1168:                    Compressor compr; // Compressor
1169:
1170:                    if (DO_TIMING)
1171:                        stime = System.currentTimeMillis();
1172:                    // Give data to all free compressors, using the current component
1173:                    while (!finishedTileComponent[c] && !idleComps.empty()) {
1174:                        // Get an idle compressor
1175:                        compr = (Compressor) idleComps.pop();
1176:                        cIdx = compr.getIdx();
1177:                        // Get data for the compressor and wake it up
1178:                        if (DO_TIMING)
1179:                            time[c] += System.currentTimeMillis() - stime;
1180:                        srcblkT[cIdx] = src.getNextInternCodeBlock(c,
1181:                                srcblkT[cIdx]);
1182:                        if (DO_TIMING)
1183:                            stime = System.currentTimeMillis();
1184:                        if (srcblkT[cIdx] != null) {
1185:                            // Initialize thread local variables
1186:                            if ((opts[tIdx][c] & OPT_BYPASS) != 0
1187:                                    && boutT[cIdx] == null) {
1188:                                boutT[cIdx] = new BitToByteOutput(outT[cIdx]);
1189:                            }
1190:                            // Initialize output code-block and compressor thread
1191:                            if (ccb == null)
1192:                                ccb = new CBlkRateDistStats();
1193:                            compr.ccb = ccb;
1194:                            compr.c = c;
1195:                            compr.options = opts[tIdx][c];
1196:                            compr.rev = isReversible(tIdx, c);
1197:                            compr.lcType = lenCalc[tIdx][c];
1198:                            compr.tType = tType[tIdx][c];
1199:                            nBusyComps[c]++;
1200:                            ccb = null;
1201:                            // Send compressor to execution in thread pool
1202:                            tPool.runTarget(compr, completedComps[c]);
1203:                        } else {
1204:                            // We finished with all the code-blocks in the current
1205:                            // tile component
1206:                            idleComps.push(compr);
1207:                            finishedTileComponent[c] = true;
1208:                        }
1209:                    }
1210:                    // If there are threads for this component which result has not
1211:                    // been returned yet, get it
1212:                    if (nBusyComps[c] > 0) {
1213:                        synchronized (completedComps[c]) {
1214:                            // If no compressor is done, wait until one is
1215:                            if (completedComps[c].empty()) {
1216:                                try {
1217:                                    if (DO_TIMING) {
1218:                                        time[c] += System.currentTimeMillis()
1219:                                                - stime;
1220:                                    }
1221:                                    completedComps[c].wait();
1222:                                    if (DO_TIMING) {
1223:                                        stime = System.currentTimeMillis();
1224:                                    }
1225:                                } catch (InterruptedException e) {
1226:                                }
1227:                            }
1228:                            // Remove the thread from the completed queue and put it
1229:                            // on the idle queue
1230:                            compr = (Compressor) completedComps[c].pop();
1231:                            cIdx = compr.getIdx();
1232:                            nBusyComps[c]--;
1233:                            idleComps.push(compr);
1234:                            // Check targets error condition
1235:                            tPool.checkTargetErrors();
1236:                            // Get the result of compression and return that.
1237:                            if (DO_TIMING)
1238:                                time[c] += System.currentTimeMillis() - stime;
1239:                            return compr.ccb;
1240:                        }
1241:                    } else {
1242:                        // Check targets error condition
1243:                        tPool.checkTargetErrors();
1244:                        // Printing timing info if necessary
1245:                        if (DO_TIMING)
1246:                            time[c] += System.currentTimeMillis() - stime;
1247:                        // Nothing is running => no more code-blocks
1248:                        return null;
1249:                    }
1250:                }
1251:            }
1252:
1253:            /**
1254:             * Changes the current tile, given the new indexes. An
1255:             * IllegalArgumentException is thrown if the indexes do not
1256:             * correspond to a valid tile.
1257:             *
1258:             * <P>This default implementation just changes the tile in the
1259:             * source.
1260:             *
1261:             * @param x The horizontal index of the tile.
1262:             *
1263:             * @param y The vertical index of the new tile.
1264:             * */
1265:            public void setTile(int x, int y) {
1266:                super .setTile(x, y);
1267:                // Reset the tilespecific variables
1268:                if (finishedTileComponent != null) {
1269:                    for (int c = src.getNumComps() - 1; c >= 0; c--) {
1270:                        finishedTileComponent[c] = false;
1271:                    }
1272:                }
1273:            }
1274:
1275:            /**
1276:             * Advances to the next tile, in standard scan-line order (by rows
1277:             * then columns). An NoNextElementException is thrown if the
1278:             * current tile is the last one (i.e. there is no next tile).
1279:             *
1280:             * <P>This default implementation just advances to the next tile
1281:             * in the source.
1282:             * */
1283:            public void nextTile() {
1284:                // Reset the tilespecific variables
1285:                if (finishedTileComponent != null) {
1286:                    for (int c = src.getNumComps() - 1; c >= 0; c--) {
1287:                        finishedTileComponent[c] = false;
1288:                    }
1289:                }
1290:                super .nextTile();
1291:            }
1292:
1293:            /**
1294:             * Compresses the code-block in 'srcblk' and puts the results in 'ccb',
1295:             * using the specified options and temporary storage.
1296:             *
1297:             * @param c The component for which to return the next code-block.
1298:             *
1299:             * @param ccb The object where the compressed data will be stored. If the
1300:             * 'data' array of 'cbb' is not null it may be reused to return the
1301:             * compressed data.
1302:             *
1303:             * @param srcblk The code-block data to code
1304:             *
1305:             * @param mq The MQ-coder to use
1306:             *
1307:             * @param bout The bit level output to use. Used only if 'OPT_BYPASS' is
1308:             * turned on in the 'options' argument.
1309:             *
1310:             * @param out The byte buffer trough which the compressed data is stored.
1311:             *
1312:             * @param state The state information for the code-block
1313:             *
1314:             * @param distbuf The buffer where to store the distortion  at
1315:             * the end of each coding pass.
1316:             *
1317:             * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at
1318:             * the end of each coding pass.
1319:             *
1320:             * @param istermbuf The buffer where to store the terminated flag for each
1321:             * coding pass.
1322:             *
1323:             * @param symbuf The buffer to hold symbols to send to the MQ coder
1324:             *
1325:             * @param ctxtbuf A buffer to hold the contexts to use in sending the
1326:             * buffered symbols to the MQ coder.
1327:             *
1328:             * @param options The options to use when coding this code-block
1329:             *
1330:             * @param rev The reversible flag. Should be true if the source of this
1331:             * code-block's data is reversible.
1332:             *
1333:             * @param lcType The type of length calculation to use with the MQ coder.
1334:             *
1335:             * @param tType The type of termination to use with the MQ coder.
1336:             *
1337:             * @see #getNextCodeBlock
1338:             * */
1339:            static private void compressCodeBlock(int c, CBlkRateDistStats ccb,
1340:                    CBlkWTData srcblk, MQCoder mq, BitToByteOutput bout,
1341:                    ByteOutputBuffer out, int state[], double distbuf[],
1342:                    int ratebuf[], boolean istermbuf[], int symbuf[],
1343:                    int ctxtbuf[], int options, boolean rev, int lcType,
1344:                    int tType) {
1345:                // NOTE: This method should not access any non-final instance or
1346:                // static variables, either directly or indirectly through other
1347:                // methods in order to be sure that the method is thread safe.
1348:
1349:                int zc_lut[]; // The ZC lookup table to use
1350:                int skipbp; // The number of non-significant bit-planes to skip
1351:                int curbp; // The current magnitude bit-plane (starts at 30)
1352:                int fm[]; // The distortion estimation lookup table for MR
1353:                int fs[]; // The distortion estimation lookup table for SC
1354:                int lmb; // The least significant magnitude bit
1355:                int npass; // The number of coding passes, for R-D statistics
1356:                double msew; // The distortion (MSE weight) for the current bit-plane
1357:                double totdist;// The total cumulative distortion decrease
1358:                int ltpidx; // The index of the last pass which is terminated
1359:
1360:                // Check error-resilient termination
1361:                if ((options & OPT_PRED_TERM) != 0
1362:                        && tType != MQCoder.TERM_PRED_ER) {
1363:                    throw new IllegalArgumentException(
1364:                            "Embedded error-resilient info "
1365:                                    + "in MQ termination option "
1366:                                    + "specified but incorrect MQ "
1367:                                    + "termination " + "policy specified");
1368:                }
1369:                // Set MQ flags
1370:                mq.setLenCalcType(lcType);
1371:                mq.setTermType(tType);
1372:
1373:                lmb = 30 - srcblk.magbits + 1;
1374:                // If there are are more bit-planes to code than the implementation
1375:                // bitdepth set lmb to 0
1376:                lmb = (lmb < 0) ? 0 : lmb;
1377:
1378:                // Reset state
1379:                ArrayUtil.intArraySet(state, 0);
1380:
1381:                // Find the most significant bit-plane
1382:                skipbp = calcSkipMSBP(srcblk, lmb);
1383:
1384:                // Initialize output code-block
1385:                ccb.m = srcblk.m;
1386:                ccb.n = srcblk.n;
1387:                ccb.sb = srcblk.sb;
1388:                ccb.nROIcoeff = srcblk.nROIcoeff;
1389:                ccb.skipMSBP = skipbp;
1390:                if (ccb.nROIcoeff != 0) {
1391:                    ccb.nROIcp = 3 * (srcblk.nROIbp - skipbp - 1) + 1;
1392:                } else {
1393:                    ccb.nROIcp = 0;
1394:                }
1395:
1396:                // Choose correct ZC lookup table for global orientation
1397:                switch (srcblk.sb.orientation) {
1398:                case Subband.WT_ORIENT_HL:
1399:                    zc_lut = ZC_LUT_HL;
1400:                    break;
1401:                case Subband.WT_ORIENT_LL:
1402:                case Subband.WT_ORIENT_LH:
1403:                    zc_lut = ZC_LUT_LH;
1404:                    break;
1405:                case Subband.WT_ORIENT_HH:
1406:                    zc_lut = ZC_LUT_HH;
1407:                    break;
1408:                default:
1409:                    throw new Error("JJ2000 internal error");
1410:                }
1411:
1412:                // Loop on significant magnitude bit-planes doing the 3 passes
1413:                curbp = 30 - skipbp;
1414:                fs = FS_LOSSY;
1415:                fm = FM_LOSSY;
1416:                msew = Math.pow(2, ((curbp - lmb) << 1) - MSE_LKP_FRAC_BITS)
1417:                        * srcblk.sb.stepWMSE * srcblk.wmseScaling;
1418:                totdist = 0f;
1419:                npass = 0;
1420:                ltpidx = -1;
1421:                // First significant bit-plane has only the pass pass
1422:                if (curbp >= lmb) {
1423:                    // Do we need the "lossless" 'fs' table ?
1424:                    if (rev && curbp == lmb) {
1425:                        fs = FM_LOSSLESS;
1426:                    }
1427:                    // We terminate if regular termination, last bit-plane, or next
1428:                    // bit-plane is "raw".
1429:                    istermbuf[npass] = (options & OPT_TERM_PASS) != 0
1430:                            || curbp == lmb
1431:                            || ((options & OPT_BYPASS) != 0 && (31 - NUM_NON_BYPASS_MS_BP - skipbp) >= curbp);
1432:                    totdist += cleanuppass(srcblk, mq, istermbuf[npass], curbp,
1433:                            state, fs, zc_lut, symbuf, ctxtbuf, ratebuf, npass,
1434:                            ltpidx, options)
1435:                            * msew;
1436:                    distbuf[npass] = totdist;
1437:                    if (istermbuf[npass])
1438:                        ltpidx = npass;
1439:                    npass++;
1440:                    msew *= 0.25;
1441:                    curbp--;
1442:                }
1443:                // Other bit-planes have all passes
1444:                while (curbp >= lmb) {
1445:                    // Do we need the "lossless" 'fs' and 'fm' tables ?
1446:                    if (rev && curbp == lmb) {
1447:                        fs = FS_LOSSLESS;
1448:                        fm = FM_LOSSLESS;
1449:                    }
1450:
1451:                    // Do the significance propagation pass
1452:                    // We terminate if regular termination only
1453:                    istermbuf[npass] = (options & OPT_TERM_PASS) != 0;
1454:                    if ((options & OPT_BYPASS) == 0
1455:                            || (31 - NUM_NON_BYPASS_MS_BP - skipbp <= curbp)) { // No bypass coding
1456:                        totdist += sigProgPass(srcblk, mq, istermbuf[npass],
1457:                                curbp, state, fs, zc_lut, symbuf, ctxtbuf,
1458:                                ratebuf, npass, ltpidx, options)
1459:                                * msew;
1460:                    } else { // Bypass ("raw") coding
1461:                        bout.setPredTerm((options & OPT_PRED_TERM) != 0);
1462:                        totdist += rawSigProgPass(srcblk, bout,
1463:                                istermbuf[npass], curbp, state, fs, ratebuf,
1464:                                npass, ltpidx, options)
1465:                                * msew;
1466:                    }
1467:                    distbuf[npass] = totdist;
1468:                    if (istermbuf[npass])
1469:                        ltpidx = npass;
1470:                    npass++;
1471:
1472:                    // Do the magnitude refinement pass
1473:                    // We terminate if regular termination or bypass ("raw") coding
1474:                    istermbuf[npass] = (options & OPT_TERM_PASS) != 0
1475:                            || ((options & OPT_BYPASS) != 0 && (31
1476:                                    - NUM_NON_BYPASS_MS_BP - skipbp > curbp));
1477:                    if ((options & OPT_BYPASS) == 0
1478:                            || (31 - NUM_NON_BYPASS_MS_BP - skipbp <= curbp)) { // No bypass coding
1479:                        totdist += magRefPass(srcblk, mq, istermbuf[npass],
1480:                                curbp, state, fm, symbuf, ctxtbuf, ratebuf,
1481:                                npass, ltpidx, options)
1482:                                * msew;
1483:                    } else { // Bypass ("raw") coding
1484:                        bout.setPredTerm((options & OPT_PRED_TERM) != 0);
1485:                        totdist += rawMagRefPass(srcblk, bout,
1486:                                istermbuf[npass], curbp, state, fm, ratebuf,
1487:                                npass, ltpidx, options)
1488:                                * msew;
1489:                    }
1490:                    distbuf[npass] = totdist;
1491:                    if (istermbuf[npass])
1492:                        ltpidx = npass;
1493:                    npass++;
1494:
1495:                    // Do the clenup pass
1496:                    // We terminate if regular termination, last bit-plane, or next
1497:                    // bit-plane is "raw".
1498:                    istermbuf[npass] = (options & OPT_TERM_PASS) != 0
1499:                            || curbp == lmb
1500:                            || ((options & OPT_BYPASS) != 0 && (31 - NUM_NON_BYPASS_MS_BP - skipbp) >= curbp);
1501:                    totdist += cleanuppass(srcblk, mq, istermbuf[npass], curbp,
1502:                            state, fs, zc_lut, symbuf, ctxtbuf, ratebuf, npass,
1503:                            ltpidx, options)
1504:                            * msew;
1505:                    distbuf[npass] = totdist;
1506:                    if (istermbuf[npass])
1507:                        ltpidx = npass;
1508:                    npass++;
1509:
1510:                    // Goto next bit-plane
1511:                    msew *= 0.25;
1512:                    curbp--;
1513:                }
1514:
1515:                // Copy compressed data and rate-distortion statistics to output
1516:                ccb.data = new byte[out.size()];
1517:                out.toByteArray(0, out.size(), ccb.data, 0);
1518:                checkEndOfPassFF(ccb.data, ratebuf, istermbuf, npass);
1519:                ccb
1520:                        .selectConvexHull(
1521:                                ratebuf,
1522:                                distbuf,
1523:                                (options & (OPT_BYPASS | OPT_TERM_PASS)) != 0 ? istermbuf
1524:                                        : null, npass, rev);
1525:
1526:                // Reset MQ coder and bit output for next code-block
1527:                mq.reset();
1528:                if (bout != null)
1529:                    bout.reset();
1530:
1531:                // Done
1532:            }
1533:
1534:            /**
1535:             * Calculates the number of magnitude bit-planes that are to be skipped,
1536:             * because they are non-significant. The algorithm looks for the largest
1537:             * magnitude and calculates the most significant bit-plane of it.
1538:             *
1539:             * @param cblk The code-block of data to scan
1540:             *
1541:             * @param lmb The least significant magnitude bit in the data
1542:             *
1543:             * @return The number of magnitude bit-planes to skip (i.e. all zero most
1544:             * significant bit-planes).
1545:             **/
1546:            static private int calcSkipMSBP(CBlkWTData cblk, int lmb) {
1547:                int k, kmax, mask;
1548:                int data[];
1549:                int maxmag;
1550:                int mag;
1551:                int w, h;
1552:                int msbp;
1553:                int l;
1554:
1555:                data = (int[]) cblk.getData();
1556:                w = cblk.w;
1557:                h = cblk.h;
1558:
1559:                // First look for the maximum magnitude in the code-block
1560:                maxmag = 0;
1561:                // Consider only magnitude bits that are in non-fractional bit-planes.
1562:                mask = 0x7FFFFFFF & (~((1 << lmb) - 1));
1563:                for (l = h - 1, k = cblk.offset; l >= 0; l--) {
1564:                    for (kmax = k + w; k < kmax; k++) {
1565:                        mag = data[k] & mask;
1566:                        if (mag > maxmag)
1567:                            maxmag = mag;
1568:                    }
1569:                    k += cblk.scanw - w;
1570:                }
1571:                // Now calculate the number of all zero most significant bit-planes for
1572:                // the maximum magnitude.
1573:                msbp = 30;
1574:                do {
1575:                    if (((1 << msbp) & maxmag) != 0)
1576:                        break;
1577:                    msbp--;
1578:                } while (msbp >= lmb);
1579:
1580:                // Return the number of non-significant bit-planes to skip
1581:                return 30 - msbp;
1582:            }
1583:
1584:            /**
1585:             * Performs the significance propagation pass on the specified data and
1586:             * bit-plane. It codes all insignificant samples which have, at least, one
1587:             * of its immediate eight neighbors already significant, using the ZC and
1588:             * SC primitives as needed. It toggles the "visited" state bit to 1 for
1589:             * all those samples.
1590:             *
1591:             * @param srcblk The code-block data to code
1592:             *
1593:             * @param mq The MQ-coder to use
1594:             *
1595:             * @param doterm If true it performs an MQ-coder termination after the end
1596:             * of the pass
1597:             *
1598:             * @param bp The bit-plane to code
1599:             *
1600:             * @param state The state information for the code-block
1601:             *
1602:             * @param fs The distortion estimation lookup table for SC
1603:             *
1604:             * @param zc_lut The ZC lookup table to use in ZC.
1605:             *
1606:             * @param symbuf The buffer to hold symbols to send to the MQ coder
1607:             *
1608:             * @param ctxtbuf A buffer to hold the contexts to use in sending the
1609:             * buffered symbols to the MQ coder.
1610:             *
1611:             * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at
1612:             * the end of this coding pass.
1613:             *
1614:             * @param pidx The coding pass index. Is the index in the 'ratebuf' array
1615:             * where to store the coded length after this coding pass.
1616:             *
1617:             * @param ltpidx The index of the last pass that was terminated, or
1618:             * negative if none.
1619:             *
1620:             * @param options The bitmask of entropy coding options to apply to the
1621:             * code-block
1622:             *
1623:             * @return The decrease in distortion for this pass, in the fixed-point
1624:             * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables.
1625:             * */
1626:            static private int sigProgPass(CBlkWTData srcblk, MQCoder mq,
1627:                    boolean doterm, int bp, int state[], int fs[],
1628:                    int zc_lut[], int symbuf[], int ctxtbuf[], int ratebuf[],
1629:                    int pidx, int ltpidx, int options) {
1630:                int j, sj; // The state index for line and stripe
1631:                int k, sk; // The data index for line and stripe
1632:                int nsym; // Symbol counter for symbol and context buffers
1633:                int dscanw; // The data scan-width
1634:                int sscanw; // The state and packed state scan-width
1635:                int jstep; // Stripe to stripe step for 'sj'
1636:                int kstep; // Stripe to stripe step for 'sk'
1637:                int stopsk; // The loop limit on the variable sk
1638:                int csj; // Local copy (i.e. cached) of 'state[j]'
1639:                int mask; // The mask for the current bit-plane
1640:                int sym; // The symbol to code
1641:                int ctxt; // The context to use
1642:                int data[]; // The data buffer
1643:                int dist; // The distortion reduction for this pass
1644:                int shift; // Shift amount for distortion
1645:                int upshift; // Shift left amount for distortion
1646:                int downshift; // Shift right amount for distortion
1647:                int normval; // The normalized sample magnitude value
1648:                int s; // The stripe index
1649:                boolean causal; // Flag to indicate if stripe-causal context
1650:                // formation is to be used
1651:                int nstripes; // The number of stripes in the code-block
1652:                int sheight; // Height of the current stripe
1653:                int off_ul, off_ur, off_dr, off_dl; // offsets
1654:
1655:                // Initialize local variables
1656:                dscanw = srcblk.scanw;
1657:                sscanw = srcblk.w + 2;
1658:                jstep = sscanw * STRIPE_HEIGHT / 2 - srcblk.w;
1659:                kstep = dscanw * STRIPE_HEIGHT - srcblk.w;
1660:                mask = 1 << bp;
1661:                data = (int[]) srcblk.getData();
1662:                nstripes = (srcblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
1663:                dist = 0;
1664:                // We use the MSE_LKP_BITS-1 bits below the bit just coded for
1665:                // distortion estimation.
1666:                shift = bp - (MSE_LKP_BITS - 1);
1667:                upshift = (shift >= 0) ? 0 : -shift;
1668:                downshift = (shift <= 0) ? 0 : shift;
1669:                causal = (options & OPT_VERT_STR_CAUSAL) != 0;
1670:
1671:                // Pre-calculate offsets in 'state' for diagonal neighbors
1672:                off_ul = -sscanw - 1; // up-left
1673:                off_ur = -sscanw + 1; // up-right
1674:                off_dr = sscanw + 1; // down-right
1675:                off_dl = sscanw - 1; // down-left
1676:
1677:                // Code stripe by stripe
1678:                sk = srcblk.offset;
1679:                sj = sscanw + 1;
1680:                for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
1681:                    sheight = (s != 0) ? STRIPE_HEIGHT : srcblk.h
1682:                            - (nstripes - 1) * STRIPE_HEIGHT;
1683:                    stopsk = sk + srcblk.w;
1684:                    // Scan by set of 1 stripe column at a time
1685:                    for (nsym = 0; sk < stopsk; sk++, sj++) {
1686:                        // Do half top of column
1687:                        j = sj;
1688:                        csj = state[j];
1689:                        // If any of the two samples is not significant and has a
1690:                        // non-zero context (i.e. some neighbor is significant) we can
1691:                        // not skip them
1692:                        if ((((~csj) & (csj << 2)) & SIG_MASK_R1R2) != 0) {
1693:                            k = sk;
1694:                            // Scan first row
1695:                            if ((csj & (STATE_SIG_R1 | STATE_NZ_CTXT_R1)) == STATE_NZ_CTXT_R1) {
1696:                                // Apply zero coding
1697:                                ctxtbuf[nsym] = zc_lut[csj & ZC_MASK];
1698:                                if ((symbuf[nsym++] = (data[k] & mask) >>> bp) != 0) {
1699:                                    // Became significant
1700:                                    // Apply sign coding
1701:                                    sym = data[k] >>> 31;
1702:                                    ctxt = SC_LUT[(csj >>> SC_SHIFT_R1)
1703:                                            & SC_MASK];
1704:                                    symbuf[nsym] = sym
1705:                                            ^ (ctxt >>> SC_SPRED_SHIFT);
1706:                                    ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
1707:                                    // Update state information (significant bit,
1708:                                    // visited bit, neighbor significant bit of
1709:                                    // neighbors, non zero context of neighbors, sign
1710:                                    // of neighbors)
1711:                                    if (!causal) {
1712:                                        // If in causal mode do not change contexts of
1713:                                        // previous stripe.
1714:                                        state[j + off_ul] |= STATE_NZ_CTXT_R2
1715:                                                | STATE_D_DR_R2;
1716:                                        state[j + off_ur] |= STATE_NZ_CTXT_R2
1717:                                                | STATE_D_DL_R2;
1718:                                    }
1719:                                    // Update sign state information of neighbors
1720:                                    if (sym != 0) {
1721:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
1722:                                                | STATE_NZ_CTXT_R2
1723:                                                | STATE_V_U_R2
1724:                                                | STATE_V_U_SIGN_R2;
1725:                                        if (!causal) {
1726:                                            // If in causal mode do not change
1727:                                            // contexts of previous stripe.
1728:                                            state[j - sscanw] |= STATE_NZ_CTXT_R2
1729:                                                    | STATE_V_D_R2
1730:                                                    | STATE_V_D_SIGN_R2;
1731:                                        }
1732:                                        state[j + 1] |= STATE_NZ_CTXT_R1
1733:                                                | STATE_NZ_CTXT_R2
1734:                                                | STATE_H_L_R1
1735:                                                | STATE_H_L_SIGN_R1
1736:                                                | STATE_D_UL_R2;
1737:                                        state[j - 1] |= STATE_NZ_CTXT_R1
1738:                                                | STATE_NZ_CTXT_R2
1739:                                                | STATE_H_R_R1
1740:                                                | STATE_H_R_SIGN_R1
1741:                                                | STATE_D_UR_R2;
1742:                                    } else {
1743:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
1744:                                                | STATE_NZ_CTXT_R2
1745:                                                | STATE_V_U_R2;
1746:                                        if (!causal) {
1747:                                            // If in causal mode do not change
1748:                                            // contexts of previous stripe.
1749:                                            state[j - sscanw] |= STATE_NZ_CTXT_R2
1750:                                                    | STATE_V_D_R2;
1751:                                        }
1752:                                        state[j + 1] |= STATE_NZ_CTXT_R1
1753:                                                | STATE_NZ_CTXT_R2
1754:                                                | STATE_H_L_R1 | STATE_D_UL_R2;
1755:                                        state[j - 1] |= STATE_NZ_CTXT_R1
1756:                                                | STATE_NZ_CTXT_R2
1757:                                                | STATE_H_R_R1 | STATE_D_UR_R2;
1758:                                    }
1759:                                    // Update distortion
1760:                                    normval = (data[k] >> downshift) << upshift;
1761:                                    dist += fs[normval
1762:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
1763:                                } else {
1764:                                    csj |= STATE_VISITED_R1;
1765:                                }
1766:                            }
1767:                            if (sheight < 2) {
1768:                                state[j] = csj;
1769:                                continue;
1770:                            }
1771:                            // Scan second row
1772:                            if ((csj & (STATE_SIG_R2 | STATE_NZ_CTXT_R2)) == STATE_NZ_CTXT_R2) {
1773:                                k += dscanw;
1774:                                // Apply zero coding
1775:                                ctxtbuf[nsym] = zc_lut[(csj >>> STATE_SEP)
1776:                                        & ZC_MASK];
1777:                                if ((symbuf[nsym++] = (data[k] & mask) >>> bp) != 0) {
1778:                                    // Became significant
1779:                                    // Apply sign coding
1780:                                    sym = data[k] >>> 31;
1781:                                    ctxt = SC_LUT[(csj >>> SC_SHIFT_R2)
1782:                                            & SC_MASK];
1783:                                    symbuf[nsym] = sym
1784:                                            ^ (ctxt >>> SC_SPRED_SHIFT);
1785:                                    ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
1786:                                    // Update state information (significant bit,
1787:                                    // visited bit, neighbor significant bit of
1788:                                    // neighbors, non zero context of neighbors, sign
1789:                                    // of neighbors)
1790:                                    state[j + off_dl] |= STATE_NZ_CTXT_R1
1791:                                            | STATE_D_UR_R1;
1792:                                    state[j + off_dr] |= STATE_NZ_CTXT_R1
1793:                                            | STATE_D_UL_R1;
1794:                                    // Update sign state information of neighbors
1795:                                    if (sym != 0) {
1796:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
1797:                                                | STATE_NZ_CTXT_R1
1798:                                                | STATE_V_D_R1
1799:                                                | STATE_V_D_SIGN_R1;
1800:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
1801:                                                | STATE_V_U_R1
1802:                                                | STATE_V_U_SIGN_R1;
1803:                                        state[j + 1] |= STATE_NZ_CTXT_R1
1804:                                                | STATE_NZ_CTXT_R2
1805:                                                | STATE_D_DL_R1 | STATE_H_L_R2
1806:                                                | STATE_H_L_SIGN_R2;
1807:                                        state[j - 1] |= STATE_NZ_CTXT_R1
1808:                                                | STATE_NZ_CTXT_R2
1809:                                                | STATE_D_DR_R1 | STATE_H_R_R2
1810:                                                | STATE_H_R_SIGN_R2;
1811:                                    } else {
1812:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
1813:                                                | STATE_NZ_CTXT_R1
1814:                                                | STATE_V_D_R1;
1815:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
1816:                                                | STATE_V_U_R1;
1817:                                        state[j + 1] |= STATE_NZ_CTXT_R1
1818:                                                | STATE_NZ_CTXT_R2
1819:                                                | STATE_D_DL_R1 | STATE_H_L_R2;
1820:                                        state[j - 1] |= STATE_NZ_CTXT_R1
1821:                                                | STATE_NZ_CTXT_R2
1822:                                                | STATE_D_DR_R1 | STATE_H_R_R2;
1823:                                    }
1824:                                    // Update distortion
1825:                                    normval = (data[k] >> downshift) << upshift;
1826:                                    dist += fs[normval
1827:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
1828:                                } else {
1829:                                    csj |= STATE_VISITED_R2;
1830:                                }
1831:                            }
1832:                            state[j] = csj;
1833:                        }
1834:                        // Do half bottom of column
1835:                        if (sheight < 3)
1836:                            continue;
1837:                        j += sscanw;
1838:                        csj = state[j];
1839:                        // If any of the two samples is not significant and has a
1840:                        // non-zero context (i.e. some neighbor is significant) we can
1841:                        // not skip them
1842:                        if ((((~csj) & (csj << 2)) & SIG_MASK_R1R2) != 0) {
1843:                            k = sk + (dscanw << 1);
1844:                            // Scan first row
1845:                            if ((csj & (STATE_SIG_R1 | STATE_NZ_CTXT_R1)) == STATE_NZ_CTXT_R1) {
1846:                                // Apply zero coding
1847:                                ctxtbuf[nsym] = zc_lut[csj & ZC_MASK];
1848:                                if ((symbuf[nsym++] = (data[k] & mask) >>> bp) != 0) {
1849:                                    // Became significant
1850:                                    // Apply sign coding
1851:                                    sym = data[k] >>> 31;
1852:                                    ctxt = SC_LUT[(csj >>> SC_SHIFT_R1)
1853:                                            & SC_MASK];
1854:                                    symbuf[nsym] = sym
1855:                                            ^ (ctxt >>> SC_SPRED_SHIFT);
1856:                                    ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
1857:                                    // Update state information (significant bit,
1858:                                    // visited bit, neighbor significant bit of
1859:                                    // neighbors, non zero context of neighbors, sign
1860:                                    // of neighbors)
1861:                                    state[j + off_ul] |= STATE_NZ_CTXT_R2
1862:                                            | STATE_D_DR_R2;
1863:                                    state[j + off_ur] |= STATE_NZ_CTXT_R2
1864:                                            | STATE_D_DL_R2;
1865:                                    // Update sign state information of neighbors
1866:                                    if (sym != 0) {
1867:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
1868:                                                | STATE_NZ_CTXT_R2
1869:                                                | STATE_V_U_R2
1870:                                                | STATE_V_U_SIGN_R2;
1871:                                        state[j - sscanw] |= STATE_NZ_CTXT_R2
1872:                                                | STATE_V_D_R2
1873:                                                | STATE_V_D_SIGN_R2;
1874:                                        state[j + 1] |= STATE_NZ_CTXT_R1
1875:                                                | STATE_NZ_CTXT_R2
1876:                                                | STATE_H_L_R1
1877:                                                | STATE_H_L_SIGN_R1
1878:                                                | STATE_D_UL_R2;
1879:                                        state[j - 1] |= STATE_NZ_CTXT_R1
1880:                                                | STATE_NZ_CTXT_R2
1881:                                                | STATE_H_R_R1
1882:                                                | STATE_H_R_SIGN_R1
1883:                                                | STATE_D_UR_R2;
1884:                                    } else {
1885:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
1886:                                                | STATE_NZ_CTXT_R2
1887:                                                | STATE_V_U_R2;
1888:                                        state[j - sscanw] |= STATE_NZ_CTXT_R2
1889:                                                | STATE_V_D_R2;
1890:                                        state[j + 1] |= STATE_NZ_CTXT_R1
1891:                                                | STATE_NZ_CTXT_R2
1892:                                                | STATE_H_L_R1 | STATE_D_UL_R2;
1893:                                        state[j - 1] |= STATE_NZ_CTXT_R1
1894:                                                | STATE_NZ_CTXT_R2
1895:                                                | STATE_H_R_R1 | STATE_D_UR_R2;
1896:                                    }
1897:                                    // Update distortion
1898:                                    normval = (data[k] >> downshift) << upshift;
1899:                                    dist += fs[normval
1900:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
1901:                                } else {
1902:                                    csj |= STATE_VISITED_R1;
1903:                                }
1904:                            }
1905:                            if (sheight < 4) {
1906:                                state[j] = csj;
1907:                                continue;
1908:                            }
1909:                            // Scan second row
1910:                            if ((csj & (STATE_SIG_R2 | STATE_NZ_CTXT_R2)) == STATE_NZ_CTXT_R2) {
1911:                                k += dscanw;
1912:                                // Apply zero coding
1913:                                ctxtbuf[nsym] = zc_lut[(csj >>> STATE_SEP)
1914:                                        & ZC_MASK];
1915:                                if ((symbuf[nsym++] = (data[k] & mask) >>> bp) != 0) {
1916:                                    // Became significant
1917:                                    // Apply sign coding
1918:                                    sym = data[k] >>> 31;
1919:                                    ctxt = SC_LUT[(csj >>> SC_SHIFT_R2)
1920:                                            & SC_MASK];
1921:                                    symbuf[nsym] = sym
1922:                                            ^ (ctxt >>> SC_SPRED_SHIFT);
1923:                                    ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
1924:                                    // Update state information (significant bit,
1925:                                    // visited bit, neighbor significant bit of
1926:                                    // neighbors, non zero context of neighbors, sign
1927:                                    // of neighbors)
1928:                                    state[j + off_dl] |= STATE_NZ_CTXT_R1
1929:                                            | STATE_D_UR_R1;
1930:                                    state[j + off_dr] |= STATE_NZ_CTXT_R1
1931:                                            | STATE_D_UL_R1;
1932:                                    // Update sign state information of neighbors
1933:                                    if (sym != 0) {
1934:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
1935:                                                | STATE_NZ_CTXT_R1
1936:                                                | STATE_V_D_R1
1937:                                                | STATE_V_D_SIGN_R1;
1938:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
1939:                                                | STATE_V_U_R1
1940:                                                | STATE_V_U_SIGN_R1;
1941:                                        state[j + 1] |= STATE_NZ_CTXT_R1
1942:                                                | STATE_NZ_CTXT_R2
1943:                                                | STATE_D_DL_R1 | STATE_H_L_R2
1944:                                                | STATE_H_L_SIGN_R2;
1945:                                        state[j - 1] |= STATE_NZ_CTXT_R1
1946:                                                | STATE_NZ_CTXT_R2
1947:                                                | STATE_D_DR_R1 | STATE_H_R_R2
1948:                                                | STATE_H_R_SIGN_R2;
1949:                                    } else {
1950:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
1951:                                                | STATE_NZ_CTXT_R1
1952:                                                | STATE_V_D_R1;
1953:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
1954:                                                | STATE_V_U_R1;
1955:                                        state[j + 1] |= STATE_NZ_CTXT_R1
1956:                                                | STATE_NZ_CTXT_R2
1957:                                                | STATE_D_DL_R1 | STATE_H_L_R2;
1958:                                        state[j - 1] |= STATE_NZ_CTXT_R1
1959:                                                | STATE_NZ_CTXT_R2
1960:                                                | STATE_D_DR_R1 | STATE_H_R_R2;
1961:                                    }
1962:                                    // Update distortion
1963:                                    normval = (data[k] >> downshift) << upshift;
1964:                                    dist += fs[normval
1965:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
1966:                                } else {
1967:                                    csj |= STATE_VISITED_R2;
1968:                                }
1969:                            }
1970:                            state[j] = csj;
1971:                        }
1972:                    }
1973:                    // Code all buffered symbols
1974:                    mq.codeSymbols(symbuf, ctxtbuf, nsym);
1975:                }
1976:
1977:                // Reset the MQ context states if we need to
1978:                if ((options & OPT_RESET_MQ) != 0) {
1979:                    mq.resetCtxts();
1980:                }
1981:
1982:                // Terminate the MQ bit stream if we need to
1983:                if (doterm) {
1984:                    ratebuf[pidx] = mq.terminate(); // Termination has special length
1985:                } else { // Use normal length calculation
1986:                    ratebuf[pidx] = mq.getNumCodedBytes();
1987:                }
1988:                // Add length of previous segments, if any
1989:                if (ltpidx >= 0) {
1990:                    ratebuf[pidx] += ratebuf[ltpidx];
1991:                }
1992:                // Finish length calculation if needed
1993:                if (doterm) {
1994:                    mq.finishLengthCalculation(ratebuf, pidx);
1995:                }
1996:
1997:                // Return the reduction in distortion
1998:                return dist;
1999:            }
2000:
2001:            /**
2002:             * Performs the significance propagation pass on the specified data and
2003:             * bit-plane, without using the arithmetic coder. It codes all
2004:             * insignificant samples which have, at least, one of its immediate eight
2005:             * neighbors already significant, using the ZC and SC primitives as
2006:             * needed. It toggles the "visited" state bit to 1 for all those samples.
2007:             *
2008:             * <P>In this method, the arithmetic coder is bypassed, and raw bits are
2009:             * directly written in the bit stream (useful when distribution are close
2010:             * to uniform, for intance, at high bit-rates and at lossless
2011:             * compression).
2012:             *
2013:             * @param srcblk The code-block data to code
2014:             *
2015:             * @param bout The bit based output
2016:             *
2017:             * @param doterm If true the bit based output is byte aligned after the
2018:             * end of the pass.
2019:             *
2020:             * @param bp The bit-plane to code
2021:             *
2022:             * @param state The state information for the code-block
2023:             *
2024:             * @param fs The distortion estimation lookup table for SC
2025:             *
2026:             * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at
2027:             * the end of this coding pass.
2028:             *
2029:             * @param pidx The coding pass index. Is the index in the 'ratebuf' array
2030:             * where to store the coded length after this coding pass.
2031:             *
2032:             * @param ltpidx The index of the last pass that was terminated, or
2033:             * negative if none.
2034:             *
2035:             * @param options The bitmask of entropy coding options to apply to the
2036:             * code-block
2037:             *
2038:             * @return The decrease in distortion for this pass, in the fixed-point
2039:             * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables.
2040:             * */
2041:            static private int rawSigProgPass(CBlkWTData srcblk,
2042:                    BitToByteOutput bout, boolean doterm, int bp, int state[],
2043:                    int fs[], int ratebuf[], int pidx, int ltpidx, int options) {
2044:                int j, sj; // The state index for line and stripe
2045:                int k, sk; // The data index for line and stripe
2046:                int dscanw; // The data scan-width
2047:                int sscanw; // The state scan-width
2048:                int jstep; // Stripe to stripe step for 'sj'
2049:                int kstep; // Stripe to stripe step for 'sk'
2050:                int stopsk; // The loop limit on the variable sk
2051:                int csj; // Local copy (i.e. cached) of 'state[j]'
2052:                int mask; // The mask for the current bit-plane
2053:                int nsym = 0; // Number of symbol
2054:                int sym; // The symbol to code
2055:                int data[]; // The data buffer
2056:                int dist; // The distortion reduction for this pass
2057:                int shift; // Shift amount for distortion
2058:                int upshift; // Shift left amount for distortion
2059:                int downshift; // Shift right amount for distortion
2060:                int normval; // The normalized sample magnitude value
2061:                int s; // The stripe index
2062:                boolean causal; // Flag to indicate if stripe-causal context
2063:                // formation is to be used
2064:                int nstripes; // The number of stripes in the code-block
2065:                int sheight; // Height of the current stripe
2066:                int off_ul, off_ur, off_dr, off_dl; // offsets
2067:
2068:                // Initialize local variables
2069:                dscanw = srcblk.scanw;
2070:                sscanw = srcblk.w + 2;
2071:                jstep = sscanw * STRIPE_HEIGHT / 2 - srcblk.w;
2072:                kstep = dscanw * STRIPE_HEIGHT - srcblk.w;
2073:                mask = 1 << bp;
2074:                data = (int[]) srcblk.getData();
2075:                nstripes = (srcblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
2076:                dist = 0;
2077:                // We use the MSE_LKP_BITS-1 bits below the bit just coded for
2078:                // distortion estimation.
2079:                shift = bp - (MSE_LKP_BITS - 1);
2080:                upshift = (shift >= 0) ? 0 : -shift;
2081:                downshift = (shift <= 0) ? 0 : shift;
2082:                causal = (options & OPT_VERT_STR_CAUSAL) != 0;
2083:
2084:                // Pre-calculate offsets in 'state' for neighbors
2085:                off_ul = -sscanw - 1; // up-left
2086:                off_ur = -sscanw + 1; // up-right
2087:                off_dr = sscanw + 1; // down-right
2088:                off_dl = sscanw - 1; // down-left
2089:
2090:                // Code stripe by stripe
2091:                sk = srcblk.offset;
2092:                sj = sscanw + 1;
2093:                for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
2094:                    sheight = (s != 0) ? STRIPE_HEIGHT : srcblk.h
2095:                            - (nstripes - 1) * STRIPE_HEIGHT;
2096:                    stopsk = sk + srcblk.w;
2097:                    // Scan by set of 1 stripe column at a time
2098:                    for (; sk < stopsk; sk++, sj++) {
2099:                        // Do half top of column
2100:                        j = sj;
2101:                        csj = state[j];
2102:                        // If any of the two samples is not significant and has a
2103:                        // non-zero context (i.e. some neighbor is significant) we can
2104:                        // not skip them
2105:                        if ((((~csj) & (csj << 2)) & SIG_MASK_R1R2) != 0) {
2106:                            k = sk;
2107:                            // Scan first row
2108:                            if ((csj & (STATE_SIG_R1 | STATE_NZ_CTXT_R1)) == STATE_NZ_CTXT_R1) {
2109:                                // Apply zero coding
2110:                                sym = (data[k] & mask) >>> bp;
2111:                                bout.writeBit(sym);
2112:                                nsym++;
2113:
2114:                                if (sym != 0) {
2115:                                    // Became significant
2116:                                    // Apply sign coding
2117:                                    sym = data[k] >>> 31;
2118:                                    bout.writeBit(sym);
2119:                                    nsym++;
2120:
2121:                                    // Update state information (significant bit,
2122:                                    // visited bit, neighbor significant bit of
2123:                                    // neighbors, non zero context of neighbors, sign
2124:                                    // of neighbors)
2125:                                    if (!causal) {
2126:                                        // If in causal mode do not change contexts of
2127:                                        // previous stripe.
2128:                                        state[j + off_ul] |= STATE_NZ_CTXT_R2
2129:                                                | STATE_D_DR_R2;
2130:                                        state[j + off_ur] |= STATE_NZ_CTXT_R2
2131:                                                | STATE_D_DL_R2;
2132:                                    }
2133:                                    // Update sign state information of neighbors
2134:                                    if (sym != 0) {
2135:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
2136:                                                | STATE_NZ_CTXT_R2
2137:                                                | STATE_V_U_R2
2138:                                                | STATE_V_U_SIGN_R2;
2139:                                        if (!causal) {
2140:                                            // If in causal mode do not change
2141:                                            // contexts of previous stripe.
2142:                                            state[j - sscanw] |= STATE_NZ_CTXT_R2
2143:                                                    | STATE_V_D_R2
2144:                                                    | STATE_V_D_SIGN_R2;
2145:                                        }
2146:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2147:                                                | STATE_NZ_CTXT_R2
2148:                                                | STATE_H_L_R1
2149:                                                | STATE_H_L_SIGN_R1
2150:                                                | STATE_D_UL_R2;
2151:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2152:                                                | STATE_NZ_CTXT_R2
2153:                                                | STATE_H_R_R1
2154:                                                | STATE_H_R_SIGN_R1
2155:                                                | STATE_D_UR_R2;
2156:                                    } else {
2157:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
2158:                                                | STATE_NZ_CTXT_R2
2159:                                                | STATE_V_U_R2;
2160:                                        if (!causal) {
2161:                                            // If in causal mode do not change
2162:                                            // contexts of previous stripe.
2163:                                            state[j - sscanw] |= STATE_NZ_CTXT_R2
2164:                                                    | STATE_V_D_R2;
2165:                                        }
2166:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2167:                                                | STATE_NZ_CTXT_R2
2168:                                                | STATE_H_L_R1 | STATE_D_UL_R2;
2169:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2170:                                                | STATE_NZ_CTXT_R2
2171:                                                | STATE_H_R_R1 | STATE_D_UR_R2;
2172:                                    }
2173:                                    // Update distortion
2174:                                    normval = (data[k] >> downshift) << upshift;
2175:                                    dist += fs[normval
2176:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
2177:                                } else {
2178:                                    csj |= STATE_VISITED_R1;
2179:                                }
2180:                            }
2181:                            if (sheight < 2) {
2182:                                state[j] = csj;
2183:                                continue;
2184:                            }
2185:                            // Scan second row
2186:                            if ((csj & (STATE_SIG_R2 | STATE_NZ_CTXT_R2)) == STATE_NZ_CTXT_R2) {
2187:                                k += dscanw;
2188:                                // Apply zero coding
2189:                                sym = (data[k] & mask) >>> bp;
2190:                                bout.writeBit(sym);
2191:                                nsym++;
2192:                                if (sym != 0) {
2193:                                    // Became significant
2194:                                    // Apply sign coding
2195:                                    sym = data[k] >>> 31;
2196:                                    bout.writeBit(sym);
2197:                                    nsym++;
2198:                                    // Update state information (significant bit,
2199:                                    // visited bit, neighbor significant bit of
2200:                                    // neighbors, non zero context of neighbors, sign
2201:                                    // of neighbors)
2202:                                    state[j + off_dl] |= STATE_NZ_CTXT_R1
2203:                                            | STATE_D_UR_R1;
2204:                                    state[j + off_dr] |= STATE_NZ_CTXT_R1
2205:                                            | STATE_D_UL_R1;
2206:                                    // Update sign state information of neighbors
2207:                                    if (sym != 0) {
2208:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
2209:                                                | STATE_NZ_CTXT_R1
2210:                                                | STATE_V_D_R1
2211:                                                | STATE_V_D_SIGN_R1;
2212:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
2213:                                                | STATE_V_U_R1
2214:                                                | STATE_V_U_SIGN_R1;
2215:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2216:                                                | STATE_NZ_CTXT_R2
2217:                                                | STATE_D_DL_R1 | STATE_H_L_R2
2218:                                                | STATE_H_L_SIGN_R2;
2219:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2220:                                                | STATE_NZ_CTXT_R2
2221:                                                | STATE_D_DR_R1 | STATE_H_R_R2
2222:                                                | STATE_H_R_SIGN_R2;
2223:                                    } else {
2224:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
2225:                                                | STATE_NZ_CTXT_R1
2226:                                                | STATE_V_D_R1;
2227:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
2228:                                                | STATE_V_U_R1;
2229:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2230:                                                | STATE_NZ_CTXT_R2
2231:                                                | STATE_D_DL_R1 | STATE_H_L_R2;
2232:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2233:                                                | STATE_NZ_CTXT_R2
2234:                                                | STATE_D_DR_R1 | STATE_H_R_R2;
2235:                                    }
2236:                                    // Update distortion
2237:                                    normval = (data[k] >> downshift) << upshift;
2238:                                    dist += fs[normval
2239:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
2240:                                } else {
2241:                                    csj |= STATE_VISITED_R2;
2242:                                }
2243:                            }
2244:                            state[j] = csj;
2245:                        }
2246:                        // Do half bottom of column
2247:                        if (sheight < 3)
2248:                            continue;
2249:                        j += sscanw;
2250:                        csj = state[j];
2251:                        // If any of the two samples is not significant and has a
2252:                        // non-zero context (i.e. some neighbor is significant) we can
2253:                        // not skip them
2254:                        if ((((~csj) & (csj << 2)) & SIG_MASK_R1R2) != 0) {
2255:                            k = sk + (dscanw << 1);
2256:                            // Scan first row
2257:                            if ((csj & (STATE_SIG_R1 | STATE_NZ_CTXT_R1)) == STATE_NZ_CTXT_R1) {
2258:                                sym = (data[k] & mask) >>> bp;
2259:                                bout.writeBit(sym);
2260:                                nsym++;
2261:                                if (sym != 0) {
2262:                                    // Became significant
2263:                                    // Apply sign coding
2264:                                    sym = data[k] >>> 31;
2265:                                    bout.writeBit(sym);
2266:                                    nsym++;
2267:                                    // Update state information (significant bit,
2268:                                    // visited bit, neighbor significant bit of
2269:                                    // neighbors, non zero context of neighbors, sign
2270:                                    // of neighbors)
2271:                                    state[j + off_ul] |= STATE_NZ_CTXT_R2
2272:                                            | STATE_D_DR_R2;
2273:                                    state[j + off_ur] |= STATE_NZ_CTXT_R2
2274:                                            | STATE_D_DL_R2;
2275:                                    // Update sign state information of neighbors
2276:                                    if (sym != 0) {
2277:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
2278:                                                | STATE_NZ_CTXT_R2
2279:                                                | STATE_V_U_R2
2280:                                                | STATE_V_U_SIGN_R2;
2281:                                        state[j - sscanw] |= STATE_NZ_CTXT_R2
2282:                                                | STATE_V_D_R2
2283:                                                | STATE_V_D_SIGN_R2;
2284:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2285:                                                | STATE_NZ_CTXT_R2
2286:                                                | STATE_H_L_R1
2287:                                                | STATE_H_L_SIGN_R1
2288:                                                | STATE_D_UL_R2;
2289:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2290:                                                | STATE_NZ_CTXT_R2
2291:                                                | STATE_H_R_R1
2292:                                                | STATE_H_R_SIGN_R1
2293:                                                | STATE_D_UR_R2;
2294:                                    } else {
2295:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
2296:                                                | STATE_NZ_CTXT_R2
2297:                                                | STATE_V_U_R2;
2298:                                        state[j - sscanw] |= STATE_NZ_CTXT_R2
2299:                                                | STATE_V_D_R2;
2300:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2301:                                                | STATE_NZ_CTXT_R2
2302:                                                | STATE_H_L_R1 | STATE_D_UL_R2;
2303:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2304:                                                | STATE_NZ_CTXT_R2
2305:                                                | STATE_H_R_R1 | STATE_D_UR_R2;
2306:                                    }
2307:                                    // Update distortion
2308:                                    normval = (data[k] >> downshift) << upshift;
2309:                                    dist += fs[normval
2310:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
2311:                                } else {
2312:                                    csj |= STATE_VISITED_R1;
2313:                                }
2314:                            }
2315:                            if (sheight < 4) {
2316:                                state[j] = csj;
2317:                                continue;
2318:                            }
2319:                            if ((csj & (STATE_SIG_R2 | STATE_NZ_CTXT_R2)) == STATE_NZ_CTXT_R2) {
2320:                                k += dscanw;
2321:                                // Apply zero coding
2322:                                sym = (data[k] & mask) >>> bp;
2323:                                bout.writeBit(sym);
2324:                                nsym++;
2325:                                if (sym != 0) {
2326:                                    // Became significant
2327:                                    // Apply sign coding
2328:                                    sym = data[k] >>> 31;
2329:                                    bout.writeBit(sym);
2330:                                    nsym++;
2331:                                    // Update state information (significant bit,
2332:                                    // visited bit, neighbor significant bit of
2333:                                    // neighbors, non zero context of neighbors, sign
2334:                                    // of neighbors)
2335:                                    state[j + off_dl] |= STATE_NZ_CTXT_R1
2336:                                            | STATE_D_UR_R1;
2337:                                    state[j + off_dr] |= STATE_NZ_CTXT_R1
2338:                                            | STATE_D_UL_R1;
2339:                                    // Update sign state information of neighbors
2340:                                    if (sym != 0) {
2341:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
2342:                                                | STATE_NZ_CTXT_R1
2343:                                                | STATE_V_D_R1
2344:                                                | STATE_V_D_SIGN_R1;
2345:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
2346:                                                | STATE_V_U_R1
2347:                                                | STATE_V_U_SIGN_R1;
2348:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2349:                                                | STATE_NZ_CTXT_R2
2350:                                                | STATE_D_DL_R1 | STATE_H_L_R2
2351:                                                | STATE_H_L_SIGN_R2;
2352:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2353:                                                | STATE_NZ_CTXT_R2
2354:                                                | STATE_D_DR_R1 | STATE_H_R_R2
2355:                                                | STATE_H_R_SIGN_R2;
2356:                                    } else {
2357:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
2358:                                                | STATE_NZ_CTXT_R1
2359:                                                | STATE_V_D_R1;
2360:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
2361:                                                | STATE_V_U_R1;
2362:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2363:                                                | STATE_NZ_CTXT_R2
2364:                                                | STATE_D_DL_R1 | STATE_H_L_R2;
2365:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2366:                                                | STATE_NZ_CTXT_R2
2367:                                                | STATE_D_DR_R1 | STATE_H_R_R2;
2368:                                    }
2369:                                    // Update distortion
2370:                                    normval = (data[k] >> downshift) << upshift;
2371:                                    dist += fs[normval
2372:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
2373:                                } else {
2374:                                    csj |= STATE_VISITED_R2;
2375:                                }
2376:                            }
2377:                            state[j] = csj;
2378:                        }
2379:                    }
2380:                }
2381:
2382:                // Get length and terminate if needed
2383:                if (doterm) {
2384:                    ratebuf[pidx] = bout.terminate();
2385:                } else {
2386:                    ratebuf[pidx] = bout.length();
2387:                }
2388:                // Add length of previous segments, if any
2389:                if (ltpidx >= 0) {
2390:                    ratebuf[pidx] += ratebuf[ltpidx];
2391:                }
2392:
2393:                // Return the reduction in distortion
2394:                return dist;
2395:            }
2396:
2397:            /**
2398:             * Performs the magnitude refinement pass on the specified data and
2399:             * bit-plane. It codes the samples which are significant and which do not
2400:             * have the "visited" state bit turned on, using the MR primitive. The
2401:             * "visited" state bit is not mofified for any samples.
2402:             *
2403:             * @param srcblk The code-block data to code
2404:             *
2405:             * @param mq The MQ-coder to use
2406:             *
2407:             * @param doterm If true it performs an MQ-coder termination after the end
2408:             * of the pass
2409:             *
2410:             * @param bp The bit-plane to code
2411:             *
2412:             * @param state The state information for the code-block
2413:             *
2414:             * @param fm The distortion estimation lookup table for MR
2415:             *
2416:             * @param symbuf The buffer to hold symbols to send to the MQ coder
2417:             *
2418:             * @param ctxtbuf A buffer to hold the contexts to use in sending the
2419:             * buffered symbols to the MQ coder.
2420:             *
2421:             * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at
2422:             * the end of this coding pass.
2423:             *
2424:             * @param pidx The coding pass index. Is the index in the 'ratebuf' array
2425:             * where to store the coded length after this coding pass.
2426:             *
2427:             * @param ltpidx The index of the last pass that was terminated, or
2428:             * negative if none.
2429:             *
2430:             * @param options The bitmask of entropy coding options to apply to the
2431:             * code-block
2432:             *
2433:             * @return The decrease in distortion for this pass, in the fixed-point
2434:             * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables.
2435:             * */
2436:            static private int magRefPass(CBlkWTData srcblk, MQCoder mq,
2437:                    boolean doterm, int bp, int state[], int fm[],
2438:                    int symbuf[], int ctxtbuf[], int ratebuf[], int pidx,
2439:                    int ltpidx, int options) {
2440:                int j, sj; // The state index for line and stripe
2441:                int k, sk; // The data index for line and stripe
2442:                int nsym = 0; // Symbol counter for symbol and context buffers
2443:                int dscanw; // The data scan-width
2444:                int sscanw; // The state scan-width
2445:                int jstep; // Stripe to stripe step for 'sj'
2446:                int kstep; // Stripe to stripe step for 'sk'
2447:                int stopsk; // The loop limit on the variable sk
2448:                int csj; // Local copy (i.e. cached) of 'state[j]'
2449:                int mask; // The mask for the current bit-plane
2450:                int data[]; // The data buffer
2451:                int dist; // The distortion reduction for this pass
2452:                int shift; // Shift amount for distortion
2453:                int upshift; // Shift left amount for distortion
2454:                int downshift; // Shift right amount for distortion
2455:                int normval; // The normalized sample magnitude value
2456:                int s; // The stripe index
2457:                int nstripes; // The number of stripes in the code-block
2458:                int sheight; // Height of the current stripe
2459:
2460:                // Initialize local variables
2461:                dscanw = srcblk.scanw;
2462:                sscanw = srcblk.w + 2;
2463:                jstep = sscanw * STRIPE_HEIGHT / 2 - srcblk.w;
2464:                kstep = dscanw * STRIPE_HEIGHT - srcblk.w;
2465:                mask = 1 << bp;
2466:                data = (int[]) srcblk.getData();
2467:                nstripes = (srcblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
2468:                dist = 0;
2469:                // We use the bit just coded plus MSE_LKP_BITS-1 bits below the bit
2470:                // just coded for distortion estimation.
2471:                shift = bp - (MSE_LKP_BITS - 1);
2472:                upshift = (shift >= 0) ? 0 : -shift;
2473:                downshift = (shift <= 0) ? 0 : shift;
2474:
2475:                // Code stripe by stripe
2476:                sk = srcblk.offset;
2477:                sj = sscanw + 1;
2478:                for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
2479:                    sheight = (s != 0) ? STRIPE_HEIGHT : srcblk.h
2480:                            - (nstripes - 1) * STRIPE_HEIGHT;
2481:                    stopsk = sk + srcblk.w;
2482:                    // Scan by set of 1 stripe column at a time
2483:                    for (nsym = 0; sk < stopsk; sk++, sj++) {
2484:                        // Do half top of column
2485:                        j = sj;
2486:                        csj = state[j];
2487:                        // If any of the two samples is significant and not yet
2488:                        // visited in the current bit-plane we can not skip them
2489:                        if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) {
2490:                            k = sk;
2491:                            // Scan first row
2492:                            if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1) {
2493:                                // Apply MR primitive
2494:                                symbuf[nsym] = (data[k] & mask) >>> bp;
2495:                                ctxtbuf[nsym++] = MR_LUT[csj & MR_MASK];
2496:                                // Update the STATE_PREV_MR bit
2497:                                csj |= STATE_PREV_MR_R1;
2498:                                // Update distortion
2499:                                normval = (data[k] >> downshift) << upshift;
2500:                                dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
2501:                            }
2502:                            if (sheight < 2) {
2503:                                state[j] = csj;
2504:                                continue;
2505:                            }
2506:                            // Scan second row
2507:                            if ((csj & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2) {
2508:                                k += dscanw;
2509:                                // Apply MR primitive
2510:                                symbuf[nsym] = (data[k] & mask) >>> bp;
2511:                                ctxtbuf[nsym++] = MR_LUT[(csj >>> STATE_SEP)
2512:                                        & MR_MASK];
2513:                                // Update the STATE_PREV_MR bit
2514:                                csj |= STATE_PREV_MR_R2;
2515:                                // Update distortion
2516:                                normval = (data[k] >> downshift) << upshift;
2517:                                dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
2518:                            }
2519:                            state[j] = csj;
2520:                        }
2521:                        // Do half bottom of column
2522:                        if (sheight < 3)
2523:                            continue;
2524:                        j += sscanw;
2525:                        csj = state[j];
2526:                        // If any of the two samples is significant and not yet
2527:                        // visited in the current bit-plane we can not skip them
2528:                        if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) {
2529:                            k = sk + (dscanw << 1);
2530:                            // Scan first row
2531:                            if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1) {
2532:                                // Apply MR primitive
2533:                                symbuf[nsym] = (data[k] & mask) >>> bp;
2534:                                ctxtbuf[nsym++] = MR_LUT[csj & MR_MASK];
2535:                                // Update the STATE_PREV_MR bit
2536:                                csj |= STATE_PREV_MR_R1;
2537:                                // Update distortion
2538:                                normval = (data[k] >> downshift) << upshift;
2539:                                dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
2540:                            }
2541:                            if (sheight < 4) {
2542:                                state[j] = csj;
2543:                                continue;
2544:                            }
2545:                            // Scan second row
2546:                            if ((state[j] & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2) {
2547:                                k += dscanw;
2548:                                // Apply MR primitive
2549:                                symbuf[nsym] = (data[k] & mask) >>> bp;
2550:                                ctxtbuf[nsym++] = MR_LUT[(csj >>> STATE_SEP)
2551:                                        & MR_MASK];
2552:                                // Update the STATE_PREV_MR bit
2553:                                csj |= STATE_PREV_MR_R2;
2554:                                // Update distortion
2555:                                normval = (data[k] >> downshift) << upshift;
2556:                                dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
2557:                            }
2558:                            state[j] = csj;
2559:                        }
2560:                    }
2561:                    // Code all buffered symbols, if any
2562:                    if (nsym > 0)
2563:                        mq.codeSymbols(symbuf, ctxtbuf, nsym);
2564:                }
2565:
2566:                // Reset the MQ context states if we need to
2567:                if ((options & OPT_RESET_MQ) != 0) {
2568:                    mq.resetCtxts();
2569:                }
2570:
2571:                // Terminate the MQ bit stream if we need to
2572:                if (doterm) {
2573:                    ratebuf[pidx] = mq.terminate(); // Termination has special length
2574:                } else { // Use normal length calculation
2575:                    ratebuf[pidx] = mq.getNumCodedBytes();
2576:                }
2577:                // Add length of previous segments, if any
2578:                if (ltpidx >= 0) {
2579:                    ratebuf[pidx] += ratebuf[ltpidx];
2580:                }
2581:                // Finish length calculation if needed
2582:                if (doterm) {
2583:                    mq.finishLengthCalculation(ratebuf, pidx);
2584:                }
2585:
2586:                // Return the reduction in distortion
2587:                return dist;
2588:            }
2589:
2590:            /**
2591:             * Performs the magnitude refinement pass on the specified data and
2592:             * bit-plane, without using the arithmetic coder. It codes the samples
2593:             * which are significant and which do not have the "visited" state bit
2594:             * turned on, using the MR primitive. The "visited" state bit is not
2595:             * mofified for any samples.
2596:             *
2597:             * <P>In this method, the arithmetic coder is bypassed, and raw bits are
2598:             * directly written in the bit stream (useful when distribution are close
2599:             * to uniform, for intance, at high bit-rates and at lossless
2600:             * compression). The 'STATE_PREV_MR_R1' and 'STATE_PREV_MR_R2' bits are
2601:             * not set because they are used only when the arithmetic coder is not
2602:             * bypassed.
2603:             *
2604:             * @param srcblk The code-block data to code
2605:             *
2606:             * @param bout The bit based output
2607:             *
2608:             * @param doterm If true the bit based output is byte aligned after the
2609:             * end of the pass.
2610:             *
2611:             * @param bp The bit-plane to code
2612:             *
2613:             * @param state The state information for the code-block
2614:             *
2615:             * @param fm The distortion estimation lookup table for MR
2616:             *
2617:             * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at
2618:             * the end of this coding pass.
2619:             *
2620:             * @param pidx The coding pass index. Is the index in the 'ratebuf' array
2621:             * where to store the coded length after this coding pass.
2622:             *
2623:             * @param ltpidx The index of the last pass that was terminated, or
2624:             * negative if none.
2625:             *
2626:             * @param options The bitmask of entropy coding options to apply to the
2627:             * code-block
2628:             *
2629:             * @return The decrease in distortion for this pass, in the fixed-point
2630:             * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables.
2631:             * */
2632:            static private int rawMagRefPass(CBlkWTData srcblk,
2633:                    BitToByteOutput bout, boolean doterm, int bp, int state[],
2634:                    int fm[], int ratebuf[], int pidx, int ltpidx, int options) {
2635:                int j, sj; // The state index for line and stripe
2636:                int k, sk; // The data index for line and stripe
2637:                int dscanw; // The data scan-width
2638:                int sscanw; // The state scan-width
2639:                int jstep; // Stripe to stripe step for 'sj'
2640:                int kstep; // Stripe to stripe step for 'sk'
2641:                int stopsk; // The loop limit on the variable sk
2642:                int csj; // Local copy (i.e. cached) of 'state[j]'
2643:                int mask; // The mask for the current bit-plane
2644:                int data[]; // The data buffer
2645:                int dist; // The distortion reduction for this pass
2646:                int shift; // Shift amount for distortion
2647:                int upshift; // Shift left amount for distortion
2648:                int downshift; // Shift right amount for distortion
2649:                int normval; // The normalized sample magnitude value
2650:                int s; // The stripe index
2651:                int nstripes; // The number of stripes in the code-block
2652:                int sheight; // Height of the current stripe
2653:                int nsym = 0;
2654:
2655:                // Initialize local variables
2656:                dscanw = srcblk.scanw;
2657:                sscanw = srcblk.w + 2;
2658:                jstep = sscanw * STRIPE_HEIGHT / 2 - srcblk.w;
2659:                kstep = dscanw * STRIPE_HEIGHT - srcblk.w;
2660:                mask = 1 << bp;
2661:                data = (int[]) srcblk.getData();
2662:                nstripes = (srcblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
2663:                dist = 0;
2664:                // We use the bit just coded plus MSE_LKP_BITS-1 bits below the bit
2665:                // just coded for distortion estimation.
2666:                shift = bp - (MSE_LKP_BITS - 1);
2667:                upshift = (shift >= 0) ? 0 : -shift;
2668:                downshift = (shift <= 0) ? 0 : shift;
2669:
2670:                // Code stripe by stripe
2671:                sk = srcblk.offset;
2672:                sj = sscanw + 1;
2673:                for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
2674:                    sheight = (s != 0) ? STRIPE_HEIGHT : srcblk.h
2675:                            - (nstripes - 1) * STRIPE_HEIGHT;
2676:                    stopsk = sk + srcblk.w;
2677:                    // Scan by set of 1 stripe column at a time
2678:                    for (; sk < stopsk; sk++, sj++) {
2679:                        // Do half top of column
2680:                        j = sj;
2681:                        csj = state[j];
2682:                        // If any of the two samples is significant and not yet
2683:                        // visited in the current bit-plane we can not skip them
2684:                        if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) {
2685:                            k = sk;
2686:                            // Scan first row
2687:                            if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1) {
2688:                                // Code bit "raw"
2689:                                bout.writeBit((data[k] & mask) >>> bp);
2690:                                nsym++;
2691:                                // No need to set STATE_PREV_MR_R1 since all magnitude
2692:                                // refinement passes to follow are "raw"
2693:                                // Update distortion
2694:                                normval = (data[k] >> downshift) << upshift;
2695:                                dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
2696:                            }
2697:                            if (sheight < 2)
2698:                                continue;
2699:                            // Scan second row
2700:                            if ((csj & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2) {
2701:                                k += dscanw;
2702:                                // Code bit "raw"
2703:                                bout.writeBit((data[k] & mask) >>> bp);
2704:                                nsym++;
2705:                                // No need to set STATE_PREV_MR_R2 since all magnitude
2706:                                // refinement passes to follow are "raw"
2707:                                // Update distortion
2708:                                normval = (data[k] >> downshift) << upshift;
2709:                                dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
2710:                            }
2711:                        }
2712:                        // Do half bottom of column
2713:                        if (sheight < 3)
2714:                            continue;
2715:                        j += sscanw;
2716:                        csj = state[j];
2717:                        // If any of the two samples is significant and not yet
2718:                        // visited in the current bit-plane we can not skip them
2719:                        if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) {
2720:                            k = sk + (dscanw << 1);
2721:                            // Scan first row
2722:                            if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1) {
2723:                                // Code bit "raw"
2724:                                bout.writeBit((data[k] & mask) >>> bp);
2725:                                nsym++;
2726:                                // No need to set STATE_PREV_MR_R1 since all magnitude
2727:                                // refinement passes to follow are "raw"
2728:                                // Update distortion
2729:                                normval = (data[k] >> downshift) << upshift;
2730:                                dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
2731:                            }
2732:                            if (sheight < 4)
2733:                                continue;
2734:                            // Scan second row
2735:                            if ((state[j] & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2) {
2736:                                k += dscanw;
2737:                                // Code bit "raw"
2738:                                bout.writeBit((data[k] & mask) >>> bp);
2739:                                nsym++;
2740:                                // No need to set STATE_PREV_MR_R2 since all magnitude
2741:                                // refinement passes to follow are "raw"
2742:                                // Update distortion
2743:                                normval = (data[k] >> downshift) << upshift;
2744:                                dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
2745:                            }
2746:                        }
2747:                    }
2748:                }
2749:
2750:                // Get length and terminate if needed
2751:                if (doterm) {
2752:                    ratebuf[pidx] = bout.terminate();
2753:                } else {
2754:                    ratebuf[pidx] = bout.length();
2755:                }
2756:
2757:                // Add length of previous segments, if any
2758:                if (ltpidx >= 0) {
2759:                    ratebuf[pidx] += ratebuf[ltpidx];
2760:                }
2761:
2762:                // Return the reduction in distortion
2763:                return dist;
2764:            }
2765:
2766:            /**
2767:             * Performs the cleanup pass on the specified data and bit-plane. It codes
2768:             * all insignificant samples which have its "visited" state bit off, using
2769:             * the ZC, SC, and RLC primitives. It toggles the "visited" state bit to 0
2770:             * (off) for all samples in the code-block.
2771:             *
2772:             * @param srcblk The code-block data to code
2773:             *
2774:             * @param mq The MQ-coder to use
2775:             *
2776:             * @param doterm If true it performs an MQ-coder termination after the end
2777:             * of the pass
2778:             *
2779:             * @param bp The bit-plane to code
2780:             *
2781:             * @param state The state information for the code-block
2782:             *
2783:             * @param fs The distortion estimation lookup table for SC
2784:             *
2785:             * @param zc_lut The ZC lookup table to use in ZC.
2786:             *
2787:             * @param symbuf The buffer to hold symbols to send to the MQ coder
2788:             *
2789:             * @param ctxtbuf A buffer to hold the contexts to use in sending the
2790:             * buffered symbols to the MQ coder.
2791:             *
2792:             * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at
2793:             * the end of this coding pass.
2794:             *
2795:             * @param pidx The coding pass index. Is the index in the 'ratebuf' array
2796:             * where to store the coded length after this coding pass.
2797:             *
2798:             * @param ltpidx The index of the last pass that was terminated, or
2799:             * negative if none.
2800:             *
2801:             * @param options The bitmask of entropy coding options to apply to the
2802:             * code-block
2803:             *
2804:             * @return The decrease in distortion for this pass, in the fixed-point
2805:             * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables.
2806:             * */
2807:            static private int cleanuppass(CBlkWTData srcblk, MQCoder mq,
2808:                    boolean doterm, int bp, int state[], int fs[],
2809:                    int zc_lut[], int symbuf[], int ctxtbuf[], int ratebuf[],
2810:                    int pidx, int ltpidx, int options) {
2811:                // NOTE: The speedup mode of the MQ coder has been briefly tried to
2812:                // speed up the coding of insignificants RLCs, without any success
2813:                // (i.e. no speedup whatsoever). The use of the speedup mode should be
2814:                // revisisted more in depth and the implementationn of it in MQCoder
2815:                // should be reviewed for optimization opportunities.
2816:                int j, sj; // The state index for line and stripe
2817:                int k, sk; // The data index for line and stripe
2818:                int nsym = 0; // Symbol counter for symbol and context buffers
2819:                int dscanw; // The data scan-width
2820:                int sscanw; // The state scan-width
2821:                int jstep; // Stripe to stripe step for 'sj'
2822:                int kstep; // Stripe to stripe step for 'sk'
2823:                int stopsk; // The loop limit on the variable sk
2824:                int csj; // Local copy (i.e. cached) of 'state[j]'
2825:                int mask; // The mask for the current bit-plane
2826:                int sym; // The symbol to code
2827:                int rlclen; // Length of RLC
2828:                int ctxt; // The context to use
2829:                int data[]; // The data buffer
2830:                int dist; // The distortion reduction for this pass
2831:                int shift; // Shift amount for distortion
2832:                int upshift; // Shift left amount for distortion
2833:                int downshift; // Shift right amount for distortion
2834:                int normval; // The normalized sample magnitude value
2835:                int s; // The stripe index
2836:                boolean causal; // Flag to indicate if stripe-causal context
2837:                // formation is to be used
2838:                int nstripes; // The number of stripes in the code-block
2839:                int sheight; // Height of the current stripe
2840:                int off_ul, off_ur, off_dr, off_dl; // offsets
2841:
2842:                // Initialize local variables
2843:                dscanw = srcblk.scanw;
2844:                sscanw = srcblk.w + 2;
2845:                jstep = sscanw * STRIPE_HEIGHT / 2 - srcblk.w;
2846:                kstep = dscanw * STRIPE_HEIGHT - srcblk.w;
2847:                mask = 1 << bp;
2848:                data = (int[]) srcblk.getData();
2849:                nstripes = (srcblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
2850:                dist = 0;
2851:                // We use the MSE_LKP_BITS-1 bits below the bit just coded for
2852:                // distortion estimation.
2853:                shift = bp - (MSE_LKP_BITS - 1);
2854:                upshift = (shift >= 0) ? 0 : -shift;
2855:                downshift = (shift <= 0) ? 0 : shift;
2856:                causal = (options & OPT_VERT_STR_CAUSAL) != 0;
2857:
2858:                // Pre-calculate offsets in 'state' for diagonal neighbors
2859:                off_ul = -sscanw - 1; // up-left
2860:                off_ur = -sscanw + 1; // up-right
2861:                off_dr = sscanw + 1; // down-right
2862:                off_dl = sscanw - 1; // down-left
2863:
2864:                // Code stripe by stripe
2865:                sk = srcblk.offset;
2866:                sj = sscanw + 1;
2867:                for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
2868:                    sheight = (s != 0) ? STRIPE_HEIGHT : srcblk.h
2869:                            - (nstripes - 1) * STRIPE_HEIGHT;
2870:                    stopsk = sk + srcblk.w;
2871:                    // Scan by set of 1 stripe column at a time
2872:                    for (nsym = 0; sk < stopsk; sk++, sj++) {
2873:                        // Start column
2874:                        j = sj;
2875:                        csj = state[j];
2876:                        top_half: {
2877:                            // Check for RLC: if all samples are not significant, not
2878:                            // visited and do not have a non-zero context, and column is
2879:                            // full height, we do RLC.
2880:                            if (csj == 0 && state[j + sscanw] == 0
2881:                                    && sheight == STRIPE_HEIGHT) {
2882:                                k = sk;
2883:                                if ((data[k] & mask) != 0) {
2884:                                    rlclen = 0;
2885:                                } else if ((data[k += dscanw] & mask) != 0) {
2886:                                    rlclen = 1;
2887:                                } else if ((data[k += dscanw] & mask) != 0) {
2888:                                    rlclen = 2;
2889:                                    j += sscanw;
2890:                                    csj = state[j];
2891:                                } else if ((data[k += dscanw] & mask) != 0) {
2892:                                    rlclen = 3;
2893:                                    j += sscanw;
2894:                                    csj = state[j];
2895:                                } else {
2896:                                    // Code insignificant RLC
2897:                                    symbuf[nsym] = 0;
2898:                                    ctxtbuf[nsym++] = RLC_CTXT;
2899:                                    // Goto next column
2900:                                    continue;
2901:                                }
2902:                                // Code significant RLC
2903:                                symbuf[nsym] = 1;
2904:                                ctxtbuf[nsym++] = RLC_CTXT;
2905:                                // Send MSB bit index
2906:                                symbuf[nsym] = rlclen >> 1;
2907:                                ctxtbuf[nsym++] = UNIF_CTXT;
2908:                                // Send LSB bit index
2909:                                symbuf[nsym] = rlclen & 0x01;
2910:                                ctxtbuf[nsym++] = UNIF_CTXT;
2911:                                // Code sign of sample that became significant
2912:                                // Update distortion
2913:                                normval = (data[k] >> downshift) << upshift;
2914:                                dist += fs[normval
2915:                                        & ((1 << (MSE_LKP_BITS - 1)) - 1)];
2916:                                // Apply sign coding
2917:                                sym = data[k] >>> 31;
2918:                                if ((rlclen & 0x01) == 0) {
2919:                                    // Sample that became significant is first row of
2920:                                    // its column half
2921:                                    ctxt = SC_LUT[(csj >>> SC_SHIFT_R1)
2922:                                            & SC_MASK];
2923:                                    symbuf[nsym] = sym
2924:                                            ^ (ctxt >>> SC_SPRED_SHIFT);
2925:                                    ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
2926:                                    // Update state information (significant bit,
2927:                                    // visited bit, neighbor significant bit of
2928:                                    // neighbors, non zero context of neighbors, sign
2929:                                    // of neighbors)
2930:                                    if (rlclen != 0 || !causal) {
2931:                                        // If in causal mode do not change contexts of
2932:                                        // previous stripe.
2933:                                        state[j + off_ul] |= STATE_NZ_CTXT_R2
2934:                                                | STATE_D_DR_R2;
2935:                                        state[j + off_ur] |= STATE_NZ_CTXT_R2
2936:                                                | STATE_D_DL_R2;
2937:                                    }
2938:                                    // Update sign state information of neighbors
2939:                                    if (sym != 0) {
2940:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
2941:                                                | STATE_NZ_CTXT_R2
2942:                                                | STATE_V_U_R2
2943:                                                | STATE_V_U_SIGN_R2;
2944:                                        if (rlclen != 0 || !causal) {
2945:                                            // If in causal mode do not change
2946:                                            // contexts of previous stripe.
2947:                                            state[j - sscanw] |= STATE_NZ_CTXT_R2
2948:                                                    | STATE_V_D_R2
2949:                                                    | STATE_V_D_SIGN_R2;
2950:                                        }
2951:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2952:                                                | STATE_NZ_CTXT_R2
2953:                                                | STATE_H_L_R1
2954:                                                | STATE_H_L_SIGN_R1
2955:                                                | STATE_D_UL_R2;
2956:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2957:                                                | STATE_NZ_CTXT_R2
2958:                                                | STATE_H_R_R1
2959:                                                | STATE_H_R_SIGN_R1
2960:                                                | STATE_D_UR_R2;
2961:                                    } else {
2962:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
2963:                                                | STATE_NZ_CTXT_R2
2964:                                                | STATE_V_U_R2;
2965:                                        if (rlclen != 0 || !causal) {
2966:                                            // If in causal mode do not change
2967:                                            // contexts of previous stripe.
2968:                                            state[j - sscanw] |= STATE_NZ_CTXT_R2
2969:                                                    | STATE_V_D_R2;
2970:                                        }
2971:                                        state[j + 1] |= STATE_NZ_CTXT_R1
2972:                                                | STATE_NZ_CTXT_R2
2973:                                                | STATE_H_L_R1 | STATE_D_UL_R2;
2974:                                        state[j - 1] |= STATE_NZ_CTXT_R1
2975:                                                | STATE_NZ_CTXT_R2
2976:                                                | STATE_H_R_R1 | STATE_D_UR_R2;
2977:                                    }
2978:                                    // Changes to csj are saved later
2979:                                    if ((rlclen >> 1) != 0) {
2980:                                        // Sample that became significant is in bottom
2981:                                        // half of column => jump to bottom half
2982:                                        break top_half;
2983:                                    }
2984:                                    // Otherwise sample that became significant is in
2985:                                    // top half of column => continue on top half
2986:                                } else {
2987:                                    // Sample that became significant is second row of
2988:                                    // its column half
2989:                                    ctxt = SC_LUT[(csj >>> SC_SHIFT_R2)
2990:                                            & SC_MASK];
2991:                                    symbuf[nsym] = sym
2992:                                            ^ (ctxt >>> SC_SPRED_SHIFT);
2993:                                    ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
2994:                                    // Update state information (significant bit,
2995:                                    // neighbor significant bit of neighbors,
2996:                                    // non zero context of neighbors, sign of neighbors)
2997:                                    state[j + off_dl] |= STATE_NZ_CTXT_R1
2998:                                            | STATE_D_UR_R1;
2999:                                    state[j + off_dr] |= STATE_NZ_CTXT_R1
3000:                                            | STATE_D_UL_R1;
3001:                                    // Update sign state information of neighbors
3002:                                    if (sym != 0) {
3003:                                        csj |= STATE_SIG_R2 | STATE_NZ_CTXT_R1
3004:                                                | STATE_V_D_R1
3005:                                                | STATE_V_D_SIGN_R1;
3006:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
3007:                                                | STATE_V_U_R1
3008:                                                | STATE_V_U_SIGN_R1;
3009:                                        state[j + 1] |= STATE_NZ_CTXT_R1
3010:                                                | STATE_NZ_CTXT_R2
3011:                                                | STATE_D_DL_R1 | STATE_H_L_R2
3012:                                                | STATE_H_L_SIGN_R2;
3013:                                        state[j - 1] |= STATE_NZ_CTXT_R1
3014:                                                | STATE_NZ_CTXT_R2
3015:                                                | STATE_D_DR_R1 | STATE_H_R_R2
3016:                                                | STATE_H_R_SIGN_R2;
3017:                                    } else {
3018:                                        csj |= STATE_SIG_R2 | STATE_NZ_CTXT_R1
3019:                                                | STATE_V_D_R1;
3020:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
3021:                                                | STATE_V_U_R1;
3022:                                        state[j + 1] |= STATE_NZ_CTXT_R1
3023:                                                | STATE_NZ_CTXT_R2
3024:                                                | STATE_D_DL_R1 | STATE_H_L_R2;
3025:                                        state[j - 1] |= STATE_NZ_CTXT_R1
3026:                                                | STATE_NZ_CTXT_R2
3027:                                                | STATE_D_DR_R1 | STATE_H_R_R2;
3028:                                    }
3029:                                    // Save changes to csj
3030:                                    state[j] = csj;
3031:                                    if ((rlclen >> 1) != 0) {
3032:                                        // Sample that became significant is in bottom
3033:                                        // half of column => we're done with this
3034:                                        // column
3035:                                        continue;
3036:                                    }
3037:                                    // Otherwise sample that became significant is in
3038:                                    // top half of column => we're done with top
3039:                                    // column
3040:                                    j += sscanw;
3041:                                    csj = state[j];
3042:                                    break top_half;
3043:                                }
3044:                            }
3045:                            // Do half top of column
3046:                            // If any of the two samples is not significant and has
3047:                            // not been visited in the current bit-plane we can not
3048:                            // skip them
3049:                            if ((((csj >> 1) | csj) & VSTD_MASK_R1R2) != VSTD_MASK_R1R2) {
3050:                                k = sk;
3051:                                // Scan first row
3052:                                if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == 0) {
3053:                                    // Apply zero coding
3054:                                    ctxtbuf[nsym] = zc_lut[csj & ZC_MASK];
3055:                                    if ((symbuf[nsym++] = (data[k] & mask) >>> bp) != 0) {
3056:                                        // Became significant
3057:                                        // Apply sign coding
3058:                                        sym = data[k] >>> 31;
3059:                                        ctxt = SC_LUT[(csj >>> SC_SHIFT_R1)
3060:                                                & SC_MASK];
3061:                                        symbuf[nsym] = sym
3062:                                                ^ (ctxt >>> SC_SPRED_SHIFT);
3063:                                        ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
3064:                                        // Update state information (significant bit,
3065:                                        // visited bit, neighbor significant bit of
3066:                                        // neighbors, non zero context of neighbors,
3067:                                        // sign of neighbors)
3068:                                        if (!causal) {
3069:                                            // If in causal mode do not change
3070:                                            // contexts of previous stripe.
3071:                                            state[j + off_ul] |= STATE_NZ_CTXT_R2
3072:                                                    | STATE_D_DR_R2;
3073:                                            state[j + off_ur] |= STATE_NZ_CTXT_R2
3074:                                                    | STATE_D_DL_R2;
3075:                                        }
3076:                                        // Update sign state information of neighbors
3077:                                        if (sym != 0) {
3078:                                            csj |= STATE_SIG_R1
3079:                                                    | STATE_VISITED_R1
3080:                                                    | STATE_NZ_CTXT_R2
3081:                                                    | STATE_V_U_R2
3082:                                                    | STATE_V_U_SIGN_R2;
3083:                                            if (!causal) {
3084:                                                // If in causal mode do not change
3085:                                                // contexts of previous stripe.
3086:                                                state[j - sscanw] |= STATE_NZ_CTXT_R2
3087:                                                        | STATE_V_D_R2
3088:                                                        | STATE_V_D_SIGN_R2;
3089:                                            }
3090:                                            state[j + 1] |= STATE_NZ_CTXT_R1
3091:                                                    | STATE_NZ_CTXT_R2
3092:                                                    | STATE_H_L_R1
3093:                                                    | STATE_H_L_SIGN_R1
3094:                                                    | STATE_D_UL_R2;
3095:                                            state[j - 1] |= STATE_NZ_CTXT_R1
3096:                                                    | STATE_NZ_CTXT_R2
3097:                                                    | STATE_H_R_R1
3098:                                                    | STATE_H_R_SIGN_R1
3099:                                                    | STATE_D_UR_R2;
3100:                                        } else {
3101:                                            csj |= STATE_SIG_R1
3102:                                                    | STATE_VISITED_R1
3103:                                                    | STATE_NZ_CTXT_R2
3104:                                                    | STATE_V_U_R2;
3105:                                            if (!causal) {
3106:                                                // If in causal mode do not change
3107:                                                // contexts of previous stripe.
3108:                                                state[j - sscanw] |= STATE_NZ_CTXT_R2
3109:                                                        | STATE_V_D_R2;
3110:                                            }
3111:                                            state[j + 1] |= STATE_NZ_CTXT_R1
3112:                                                    | STATE_NZ_CTXT_R2
3113:                                                    | STATE_H_L_R1
3114:                                                    | STATE_D_UL_R2;
3115:                                            state[j - 1] |= STATE_NZ_CTXT_R1
3116:                                                    | STATE_NZ_CTXT_R2
3117:                                                    | STATE_H_R_R1
3118:                                                    | STATE_D_UR_R2;
3119:                                        }
3120:                                        // Update distortion
3121:                                        normval = (data[k] >> downshift) << upshift;
3122:                                        dist += fs[normval
3123:                                                & ((1 << (MSE_LKP_BITS - 1)) - 1)];
3124:                                    }
3125:                                }
3126:                                if (sheight < 2) {
3127:                                    csj &= ~(STATE_VISITED_R1 | STATE_VISITED_R2);
3128:                                    state[j] = csj;
3129:                                    continue;
3130:                                }
3131:                                // Scan second row
3132:                                if ((csj & (STATE_SIG_R2 | STATE_VISITED_R2)) == 0) {
3133:                                    k += dscanw;
3134:                                    // Apply zero coding
3135:                                    ctxtbuf[nsym] = zc_lut[(csj >>> STATE_SEP)
3136:                                            & ZC_MASK];
3137:                                    if ((symbuf[nsym++] = (data[k] & mask) >>> bp) != 0) {
3138:                                        // Became significant
3139:                                        // Apply sign coding
3140:                                        sym = data[k] >>> 31;
3141:                                        ctxt = SC_LUT[(csj >>> SC_SHIFT_R2)
3142:                                                & SC_MASK];
3143:                                        symbuf[nsym] = sym
3144:                                                ^ (ctxt >>> SC_SPRED_SHIFT);
3145:                                        ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
3146:                                        // Update state information (significant bit,
3147:                                        // visited bit, neighbor significant bit of
3148:                                        // neighbors, non zero context of neighbors,
3149:                                        // sign of neighbors)
3150:                                        state[j + off_dl] |= STATE_NZ_CTXT_R1
3151:                                                | STATE_D_UR_R1;
3152:                                        state[j + off_dr] |= STATE_NZ_CTXT_R1
3153:                                                | STATE_D_UL_R1;
3154:                                        // Update sign state information of neighbors
3155:                                        if (sym != 0) {
3156:                                            csj |= STATE_SIG_R2
3157:                                                    | STATE_VISITED_R2
3158:                                                    | STATE_NZ_CTXT_R1
3159:                                                    | STATE_V_D_R1
3160:                                                    | STATE_V_D_SIGN_R1;
3161:                                            state[j + sscanw] |= STATE_NZ_CTXT_R1
3162:                                                    | STATE_V_U_R1
3163:                                                    | STATE_V_U_SIGN_R1;
3164:                                            state[j + 1] |= STATE_NZ_CTXT_R1
3165:                                                    | STATE_NZ_CTXT_R2
3166:                                                    | STATE_D_DL_R1
3167:                                                    | STATE_H_L_R2
3168:                                                    | STATE_H_L_SIGN_R2;
3169:                                            state[j - 1] |= STATE_NZ_CTXT_R1
3170:                                                    | STATE_NZ_CTXT_R2
3171:                                                    | STATE_D_DR_R1
3172:                                                    | STATE_H_R_R2
3173:                                                    | STATE_H_R_SIGN_R2;
3174:                                        } else {
3175:                                            csj |= STATE_SIG_R2
3176:                                                    | STATE_VISITED_R2
3177:                                                    | STATE_NZ_CTXT_R1
3178:                                                    | STATE_V_D_R1;
3179:                                            state[j + sscanw] |= STATE_NZ_CTXT_R1
3180:                                                    | STATE_V_U_R1;
3181:                                            state[j + 1] |= STATE_NZ_CTXT_R1
3182:                                                    | STATE_NZ_CTXT_R2
3183:                                                    | STATE_D_DL_R1
3184:                                                    | STATE_H_L_R2;
3185:                                            state[j - 1] |= STATE_NZ_CTXT_R1
3186:                                                    | STATE_NZ_CTXT_R2
3187:                                                    | STATE_D_DR_R1
3188:                                                    | STATE_H_R_R2;
3189:                                        }
3190:                                        // Update distortion
3191:                                        normval = (data[k] >> downshift) << upshift;
3192:                                        dist += fs[normval
3193:                                                & ((1 << (MSE_LKP_BITS - 1)) - 1)];
3194:                                    }
3195:                                }
3196:                            }
3197:                            csj &= ~(STATE_VISITED_R1 | STATE_VISITED_R2);
3198:                            state[j] = csj;
3199:                            // Do half bottom of column
3200:                            if (sheight < 3)
3201:                                continue;
3202:                            j += sscanw;
3203:                            csj = state[j];
3204:                        } // end of 'top_half' block
3205:                        // If any of the two samples is not significant and has
3206:                        // not been visited in the current bit-plane we can not
3207:                        // skip them
3208:                        if ((((csj >> 1) | csj) & VSTD_MASK_R1R2) != VSTD_MASK_R1R2) {
3209:                            k = sk + (dscanw << 1);
3210:                            // Scan first row
3211:                            if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == 0) {
3212:                                // Apply zero coding
3213:                                ctxtbuf[nsym] = zc_lut[csj & ZC_MASK];
3214:                                if ((symbuf[nsym++] = (data[k] & mask) >>> bp) != 0) {
3215:                                    // Became significant
3216:                                    // Apply sign coding
3217:                                    sym = data[k] >>> 31;
3218:                                    ctxt = SC_LUT[(csj >>> SC_SHIFT_R1)
3219:                                            & SC_MASK];
3220:                                    symbuf[nsym] = sym
3221:                                            ^ (ctxt >>> SC_SPRED_SHIFT);
3222:                                    ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
3223:                                    // Update state information (significant bit,
3224:                                    // visited bit, neighbor significant bit of
3225:                                    // neighbors, non zero context of neighbors,
3226:                                    // sign of neighbors)
3227:                                    state[j + off_ul] |= STATE_NZ_CTXT_R2
3228:                                            | STATE_D_DR_R2;
3229:                                    state[j + off_ur] |= STATE_NZ_CTXT_R2
3230:                                            | STATE_D_DL_R2;
3231:                                    // Update sign state information of neighbors
3232:                                    if (sym != 0) {
3233:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
3234:                                                | STATE_NZ_CTXT_R2
3235:                                                | STATE_V_U_R2
3236:                                                | STATE_V_U_SIGN_R2;
3237:                                        state[j - sscanw] |= STATE_NZ_CTXT_R2
3238:                                                | STATE_V_D_R2
3239:                                                | STATE_V_D_SIGN_R2;
3240:                                        state[j + 1] |= STATE_NZ_CTXT_R1
3241:                                                | STATE_NZ_CTXT_R2
3242:                                                | STATE_H_L_R1
3243:                                                | STATE_H_L_SIGN_R1
3244:                                                | STATE_D_UL_R2;
3245:                                        state[j - 1] |= STATE_NZ_CTXT_R1
3246:                                                | STATE_NZ_CTXT_R2
3247:                                                | STATE_H_R_R1
3248:                                                | STATE_H_R_SIGN_R1
3249:                                                | STATE_D_UR_R2;
3250:                                    } else {
3251:                                        csj |= STATE_SIG_R1 | STATE_VISITED_R1
3252:                                                | STATE_NZ_CTXT_R2
3253:                                                | STATE_V_U_R2;
3254:                                        state[j - sscanw] |= STATE_NZ_CTXT_R2
3255:                                                | STATE_V_D_R2;
3256:                                        state[j + 1] |= STATE_NZ_CTXT_R1
3257:                                                | STATE_NZ_CTXT_R2
3258:                                                | STATE_H_L_R1 | STATE_D_UL_R2;
3259:                                        state[j - 1] |= STATE_NZ_CTXT_R1
3260:                                                | STATE_NZ_CTXT_R2
3261:                                                | STATE_H_R_R1 | STATE_D_UR_R2;
3262:                                    }
3263:                                    // Update distortion
3264:                                    normval = (data[k] >> downshift) << upshift;
3265:                                    dist += fs[normval
3266:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
3267:                                }
3268:                            }
3269:                            if (sheight < 4) {
3270:                                csj &= ~(STATE_VISITED_R1 | STATE_VISITED_R2);
3271:                                state[j] = csj;
3272:                                continue;
3273:                            }
3274:                            // Scan second row
3275:                            if ((csj & (STATE_SIG_R2 | STATE_VISITED_R2)) == 0) {
3276:                                k += dscanw;
3277:                                // Apply zero coding
3278:                                ctxtbuf[nsym] = zc_lut[(csj >>> STATE_SEP)
3279:                                        & ZC_MASK];
3280:                                if ((symbuf[nsym++] = (data[k] & mask) >>> bp) != 0) {
3281:                                    // Became significant
3282:                                    // Apply sign coding
3283:                                    sym = data[k] >>> 31;
3284:                                    ctxt = SC_LUT[(csj >>> SC_SHIFT_R2)
3285:                                            & SC_MASK];
3286:                                    symbuf[nsym] = sym
3287:                                            ^ (ctxt >>> SC_SPRED_SHIFT);
3288:                                    ctxtbuf[nsym++] = ctxt & SC_LUT_MASK;
3289:                                    // Update state information (significant bit,
3290:                                    // visited bit, neighbor significant bit of
3291:                                    // neighbors, non zero context of neighbors,
3292:                                    // sign of neighbors)
3293:                                    state[j + off_dl] |= STATE_NZ_CTXT_R1
3294:                                            | STATE_D_UR_R1;
3295:                                    state[j + off_dr] |= STATE_NZ_CTXT_R1
3296:                                            | STATE_D_UL_R1;
3297:                                    // Update sign state information of neighbors
3298:                                    if (sym != 0) {
3299:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
3300:                                                | STATE_NZ_CTXT_R1
3301:                                                | STATE_V_D_R1
3302:                                                | STATE_V_D_SIGN_R1;
3303:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
3304:                                                | STATE_V_U_R1
3305:                                                | STATE_V_U_SIGN_R1;
3306:                                        state[j + 1] |= STATE_NZ_CTXT_R1
3307:                                                | STATE_NZ_CTXT_R2
3308:                                                | STATE_D_DL_R1 | STATE_H_L_R2
3309:                                                | STATE_H_L_SIGN_R2;
3310:                                        state[j - 1] |= STATE_NZ_CTXT_R1
3311:                                                | STATE_NZ_CTXT_R2
3312:                                                | STATE_D_DR_R1 | STATE_H_R_R2
3313:                                                | STATE_H_R_SIGN_R2;
3314:                                    } else {
3315:                                        csj |= STATE_SIG_R2 | STATE_VISITED_R2
3316:                                                | STATE_NZ_CTXT_R1
3317:                                                | STATE_V_D_R1;
3318:                                        state[j + sscanw] |= STATE_NZ_CTXT_R1
3319:                                                | STATE_V_U_R1;
3320:                                        state[j + 1] |= STATE_NZ_CTXT_R1
3321:                                                | STATE_NZ_CTXT_R2
3322:                                                | STATE_D_DL_R1 | STATE_H_L_R2;
3323:                                        state[j - 1] |= STATE_NZ_CTXT_R1
3324:                                                | STATE_NZ_CTXT_R2
3325:                                                | STATE_D_DR_R1 | STATE_H_R_R2;
3326:                                    }
3327:                                    // Update distortion
3328:                                    normval = (data[k] >> downshift) << upshift;
3329:                                    dist += fs[normval
3330:                                            & ((1 << (MSE_LKP_BITS - 1)) - 1)];
3331:                                }
3332:                            }
3333:                        }
3334:                        csj &= ~(STATE_VISITED_R1 | STATE_VISITED_R2);
3335:                        state[j] = csj;
3336:                    }
3337:                    // Code all buffered symbols, if any
3338:                    if (nsym > 0)
3339:                        mq.codeSymbols(symbuf, ctxtbuf, nsym);
3340:                }
3341:
3342:                // Insert a segment marker if we need to
3343:                if ((options & OPT_SEG_SYMBOLS) != 0) {
3344:                    mq.codeSymbols(SEG_SYMBOLS, SEG_SYMB_CTXTS,
3345:                            SEG_SYMBOLS.length);
3346:                }
3347:
3348:                // Reset the MQ context states if we need to
3349:                if ((options & OPT_RESET_MQ) != 0) {
3350:                    mq.resetCtxts();
3351:                }
3352:
3353:                // Terminate the MQ bit stream if we need to
3354:                if (doterm) {
3355:                    ratebuf[pidx] = mq.terminate(); // Termination has special length
3356:                } else { // Use normal length calculation
3357:                    ratebuf[pidx] = mq.getNumCodedBytes();
3358:                }
3359:                // Add length of previous segments, if any
3360:                if (ltpidx >= 0) {
3361:                    ratebuf[pidx] += ratebuf[ltpidx];
3362:                }
3363:                // Finish length calculation if needed
3364:                if (doterm) {
3365:                    mq.finishLengthCalculation(ratebuf, pidx);
3366:                }
3367:
3368:                // Return the reduction in distortion
3369:                return dist;
3370:            }
3371:
3372:            /**
3373:             * Ensures that at the end of a non-terminated coding pass there is not a
3374:             * 0xFF byte, modifying the stored rates if necessary.
3375:             *
3376:             * <P>Due to error resiliance reasons, a coding pass should never have its
3377:             * last byte be a 0xFF, since that can lead to the emulation of a resync
3378:             * marker. This method checks if that is the case, and reduces the rate
3379:             * for a given pass if necessary. The ommitted 0xFF will be synthetized by
3380:             * the decoder if necessary, as required by JPEG 2000. This method should
3381:             * only be called once that the entire code-block is coded.
3382:             *
3383:             * <P>Passes that are terminated are not checked for the 0xFF byte, since
3384:             * it is assumed that the termination procedure does not output any
3385:             * trailing 0xFF. Checking the terminated segments would involve much more
3386:             * than just modifying the stored rates.
3387:             *
3388:             * <P>NOTE: It is assumed by this method that the coded data does not
3389:             * contain consecutive 0xFF bytes, as is the case with the MQ and
3390:             * 'arithemetic coding bypass' bit stuffing policy. However, the
3391:             * termination policies used should also respect this requirement.
3392:             *
3393:             * @param data The coded data for the code-block
3394:             *
3395:             * @param rates The rate (i.e. accumulated number of bytes) for each
3396:             * coding pass
3397:             *
3398:             * @param isterm An array of flags indicating, for each pass, if it is
3399:             * terminated or not. If null it is assumed that no pass is terminated,
3400:             * except the last one.
3401:             *
3402:             * @param n The number of coding passes
3403:             * */
3404:            static private void checkEndOfPassFF(byte data[], int rates[],
3405:                    boolean isterm[], int n) {
3406:                int dp; // the position to test in 'data'
3407:
3408:                // If a pass ends in 0xFF we need to reduce the number of bytes in it,
3409:                // so that it does not end in 0xFF. We only need to go back one byte
3410:                // since there can be no consecutive 0xFF bytes.
3411:
3412:                // If there are no terminated passes avoid the test on 'isterm'
3413:                if (isterm == null) {
3414:                    for (n--; n >= 0; n--) {
3415:                        dp = rates[n] - 1;
3416:                        if (dp >= 0 && (data[dp] == (byte) 0xFF)) {
3417:                            rates[n]--;
3418:                        }
3419:                    }
3420:                } else {
3421:                    for (n--; n >= 0; n--) {
3422:                        if (!isterm[n]) {
3423:                            dp = rates[n] - 1;
3424:                            if (dp >= 0 && (data[dp] == (byte) 0xFF)) {
3425:                                rates[n]--;
3426:                            }
3427:                        }
3428:                    }
3429:                }
3430:            }
3431:
3432:            /**
3433:             * Load options, length calculation type and termination type for
3434:             * each tile-component.
3435:             *
3436:             * @param nt The number of tiles
3437:             *
3438:             * @param nc The number of components
3439:             * */
3440:            public void initTileComp(int nt, int nc) {
3441:
3442:                opts = new int[nt][nc];
3443:                lenCalc = new int[nt][nc];
3444:                tType = new int[nt][nc];
3445:
3446:                for (int t = 0; t < nt; t++) {
3447:                    for (int c = 0; c < nc; c++) {
3448:                        opts[t][c] = 0;
3449:
3450:                        // Bypass coding mode ?
3451:                        if (((String) bms.getTileCompVal(t, c))
3452:                                .equalsIgnoreCase("true")) {
3453:                            opts[t][c] |= OPT_BYPASS;
3454:                        }
3455:                        // MQ reset after each coding pass ?
3456:                        if (((String) mqrs.getTileCompVal(t, c))
3457:                                .equalsIgnoreCase("true")) {
3458:                            opts[t][c] |= OPT_RESET_MQ;
3459:                        }
3460:                        // MQ termination after each arithmetically coded coding pass ?
3461:                        if (((String) rts.getTileCompVal(t, c))
3462:                                .equalsIgnoreCase("true")) {
3463:                            opts[t][c] |= OPT_TERM_PASS;
3464:                        }
3465:                        // Vertically stripe-causal context mode ?
3466:                        if (((String) css.getTileCompVal(t, c))
3467:                                .equalsIgnoreCase("true")) {
3468:                            opts[t][c] |= OPT_VERT_STR_CAUSAL;
3469:                        }
3470:                        // Error resilience segmentation symbol insertion ?
3471:                        if (((String) sss.getTileCompVal(t, c))
3472:                                .equalsIgnoreCase("true")) {
3473:                            opts[t][c] |= OPT_SEG_SYMBOLS;
3474:                        }
3475:
3476:                        // Set length calculation type of the MQ coder
3477:                        String lCalcType = (String) lcs.getTileCompVal(t, c);
3478:                        if (lCalcType.equals("near_opt")) {
3479:                            lenCalc[t][c] = MQCoder.LENGTH_NEAR_OPT;
3480:                        } else if (lCalcType.equals("lazy_good")) {
3481:                            lenCalc[t][c] = MQCoder.LENGTH_LAZY_GOOD;
3482:                        } else if (lCalcType.equals("lazy")) {
3483:                            lenCalc[t][c] = MQCoder.LENGTH_LAZY;
3484:                        } else {
3485:                            throw new IllegalArgumentException(
3486:                                    "Unrecognized or " + "unsupported MQ "
3487:                                            + "length calculation.");
3488:                        }
3489:
3490:                        // Set termination type of MQ coder
3491:                        String termType = (String) tts.getTileCompVal(t, c);
3492:                        if (termType.equalsIgnoreCase("easy")) {
3493:                            tType[t][c] = MQCoder.TERM_EASY;
3494:                        } else if (termType.equalsIgnoreCase("full")) {
3495:                            tType[t][c] = MQCoder.TERM_FULL;
3496:                        } else if (termType.equalsIgnoreCase("near_opt")) {
3497:                            tType[t][c] = MQCoder.TERM_NEAR_OPT;
3498:                        } else if (termType.equalsIgnoreCase("predict")) {
3499:                            tType[t][c] = MQCoder.TERM_PRED_ER;
3500:                            opts[t][c] |= OPT_PRED_TERM;
3501:                            if ((opts[t][c] & (OPT_TERM_PASS | OPT_BYPASS)) == 0) {
3502:                                FacilityManager
3503:                                        .getMsgLogger()
3504:                                        .printmsg(
3505:                                                MsgLogger.INFO,
3506:                                                "Using error resilient MQ"
3507:                                                        + " termination, but terminating only at "
3508:                                                        + "the end of code-blocks. The error "
3509:                                                        + "protection offered by this option will"
3510:                                                        + " be very weak. Specify the 'Creg_term' "
3511:                                                        + "and/or 'Cbypass' option for "
3512:                                                        + "increased error resilience.");
3513:                            }
3514:                        } else {
3515:                            throw new IllegalArgumentException(
3516:                                    "Unrecognized or " + "unsupported "
3517:                                            + "MQ coder termination.");
3518:                        }
3519:
3520:                    } // End loop on components
3521:                } // End loop on tiles
3522:            }
3523:
3524:            /**
3525:             * Returns the precinct partition width for the specified
3526:             * component, tile and resolution level.
3527:             *
3528:             * @param t the tile index
3529:             *
3530:             * @param c the component
3531:             *
3532:             * @param rl the resolution level
3533:             *
3534:             * @return The precinct partition width for the specified
3535:             * component, tile and resolution level
3536:             * */
3537:            public int getPPX(int t, int c, int rl) {
3538:                return pss.getPPX(t, c, rl);
3539:            }
3540:
3541:            /**
3542:             * Returns the precinct partition height for the specified
3543:             * component, tile and resolution level.
3544:             *
3545:             * @param t the tile index
3546:             *
3547:             * @param c the component
3548:             *
3549:             * @param rl the resolution level
3550:             *
3551:             * @return The precinct partition height for the specified
3552:             * component, tile and resolution level
3553:             * */
3554:            public int getPPY(int t, int c, int rl) {
3555:                return pss.getPPY(t, c, rl);
3556:            }
3557:
3558:            /**
3559:             * Returns true if precinct partition is used for the specified
3560:             * component and tile, returns false otherwise.
3561:             *
3562:             * @param c The component
3563:             *
3564:             * @param t The tile
3565:             *
3566:             * @return True if precinct partition is used for the specified
3567:             * component and tile, returns false otherwise.
3568:             * */
3569:            public boolean precinctPartitionUsed(int c, int t) {
3570:                return precinctPartition[c][t];
3571:            }
3572:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.