00001 // 00002 // (C) Jan de Vaan 2007-2010, all rights reserved. See the accompanying "License.txt" for licensed use. 00003 // 00004 00005 00006 #ifndef CHARLS_CONTEXT 00007 #define CHARLS_CONTEXT 00008 00009 00010 // 00011 // JlsContext: a JPEG-LS context with it's current statistics. 00012 // 00013 struct JlsContext 00014 { 00015 public: 00016 JlsContext() 00017 {} 00018 00019 JlsContext(LONG a) : 00020 A(a), 00021 B(0), 00022 C(0), 00023 N(1) 00024 { 00025 } 00026 00027 LONG A; 00028 LONG B; 00029 short C; 00030 short N; 00031 00032 inlinehint LONG GetErrorCorrection(LONG k) const 00033 { 00034 if (k != 0) 00035 return 0; 00036 00037 return BitWiseSign(2 * B + N - 1); 00038 } 00039 00040 00041 inlinehint void UpdateVariables(LONG errorValue, LONG NEAR, LONG NRESET) 00042 { 00043 ASSERT(N != 0); 00044 00045 // For performance work on copies of A,B,N (compiler will use registers). 00046 int b = B + errorValue * (2 * NEAR + 1); 00047 int a = A + ABS(errorValue); 00048 int n = N; 00049 00050 ASSERT(a < 65536 * 256); 00051 ASSERT(ABS(b) < 65536 * 256); 00052 00053 if (n == NRESET) 00054 { 00055 a = a >> 1; 00056 b = b >> 1; 00057 n = n >> 1; 00058 } 00059 00060 n = n + 1; 00061 00062 if (b + n <= 0) 00063 { 00064 b = b + n; 00065 if (b <= -n) 00066 { 00067 b = -n + 1; 00068 } 00069 C = _tableC[C - 1]; 00070 } 00071 else if (b > 0) 00072 { 00073 b = b - n; 00074 if (b > 0) 00075 { 00076 b = 0; 00077 } 00078 C = _tableC[C + 1]; 00079 } 00080 A = a; 00081 B = b; 00082 N = (short)n; 00083 ASSERT(N != 0); 00084 } 00085 00086 00087 00088 inlinehint LONG GetGolomb() const 00089 { 00090 LONG Ntest = N; 00091 LONG Atest = A; 00092 LONG k = 0; 00093 for(; (Ntest << k) < Atest; k++) 00094 { 00095 ASSERT(k <= 32); 00096 }; 00097 return k; 00098 } 00099 00100 static signed char* CreateTableC() 00101 { 00102 static OFVector<signed char> rgtableC; 00103 00104 rgtableC.reserve(256 + 2); 00105 00106 rgtableC.push_back(-128); 00107 for (int i = -128; i < 128; i++) 00108 { 00109 rgtableC.push_back(char(i)); 00110 } 00111 rgtableC.push_back(127); 00112 00113 signed char* pZero = &rgtableC[128 + 1]; 00114 ASSERT(pZero[0] == 0); 00115 return pZero; 00116 } 00117 private: 00118 00119 static signed char* _tableC; 00120 }; 00121 00122 #endif