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_DEFAULTTRAITS 00007 #define CHARLS_DEFAULTTRAITS 00008 00009 // Default traits that support all JPEG LS parameters: custom limit, near, maxval (not power of 2) 00010 00011 // This traits class is used to initialize a coder/decoder. 00012 // The coder/decoder also delegates some functions to the traits class. 00013 // This is to allow the traits class to replace the default implementation here with optimized specific implementations. 00014 // This is done for lossless coding/decoding: see losslesstraits.h 00015 00016 template <class sample, class pixel> 00017 struct DefaultTraitsT 00018 { 00019 public: 00020 typedef sample SAMPLE; 00021 typedef pixel PIXEL; 00022 00023 LONG MAXVAL; 00024 LONG RANGE; 00025 LONG NEAR; 00026 LONG qbpp; 00027 LONG bpp; 00028 LONG LIMIT; 00029 LONG RESET; 00030 00031 DefaultTraitsT(const DefaultTraitsT& src) : 00032 MAXVAL(src.MAXVAL), 00033 RANGE(src.RANGE), 00034 NEAR(src.NEAR), 00035 qbpp(src.qbpp), 00036 bpp(src.bpp), 00037 LIMIT(src.LIMIT), 00038 RESET(src.RESET) 00039 { 00040 } 00041 00042 DefaultTraitsT(LONG max, LONG jls_near) 00043 { 00044 NEAR = jls_near; 00045 MAXVAL = max; 00046 RANGE = (MAXVAL + 2 * NEAR )/(2 * NEAR + 1) + 1; 00047 bpp = log_2(max); 00048 LIMIT = 2 * (bpp + MAX(8,bpp)); 00049 qbpp = log_2(RANGE); 00050 RESET = BASIC_RESET; 00051 } 00052 00053 00054 inlinehint LONG ComputeErrVal(LONG e) const 00055 { 00056 LONG q = Quantize(e); 00057 return ModRange(q); 00058 } 00059 00060 inlinehint SAMPLE ComputeReconstructedSample(LONG Px, LONG ErrVal) 00061 { 00062 return FixReconstructedValue(Px + DeQuantize(ErrVal)); 00063 } 00064 00065 inlinehint bool IsNear(LONG lhs, LONG rhs) const 00066 { return ABS(lhs-rhs) <=NEAR; } 00067 00068 bool IsNear(Triplet<SAMPLE> lhs, Triplet<SAMPLE> rhs) const 00069 { 00070 return ABS(lhs.v1-rhs.v1) <=NEAR && 00071 ABS(lhs.v2-rhs.v2) <=NEAR && 00072 ABS(lhs.v3-rhs.v3) <=NEAR; 00073 } 00074 00075 inlinehint LONG CorrectPrediction(LONG Pxc) const 00076 { 00077 if ((Pxc & MAXVAL) == Pxc) 00078 return Pxc; 00079 00080 return (~(Pxc >> (LONG_BITCOUNT-1))) & MAXVAL; 00081 } 00082 00083 inlinehint LONG ModRange(LONG Errval) const 00084 { 00085 ASSERT(ABS(Errval) <= RANGE); 00086 if (Errval < 0) 00087 Errval = Errval + RANGE; 00088 00089 if (Errval >= ((RANGE + 1) / 2)) 00090 Errval = Errval - RANGE; 00091 00092 ASSERT(ABS(Errval) <= RANGE/2); 00093 00094 return Errval; 00095 } 00096 00097 00098 private: 00099 LONG Quantize(LONG Errval) const 00100 { 00101 if (Errval > 0) 00102 return (Errval + NEAR) / (2 * NEAR + 1); 00103 else 00104 return - (NEAR - Errval) / (2 * NEAR + 1); 00105 } 00106 00107 00108 inlinehint LONG DeQuantize(LONG Errval) const 00109 { 00110 return Errval * (2 * NEAR + 1); 00111 } 00112 00113 inlinehint SAMPLE FixReconstructedValue(LONG val) const 00114 { 00115 if (val < -NEAR) 00116 val = val + RANGE*(2*NEAR+1); 00117 else if (val > MAXVAL + NEAR) 00118 val = val - RANGE*(2*NEAR+1); 00119 00120 return SAMPLE(CorrectPrediction(val)); 00121 } 00122 00123 }; 00124 00125 00126 #endif