1 // 2 // (C) Jan de Vaan 2007-2010, all rights reserved. See the accompanying "License.txt" for licensed use. 3 // 4 5 6 7 #ifndef CHARLS_LOSSLESSTRAITS 8 #define CHARLS_LOSSLESSTRAITS 9 10 #include "header.h" 11 12 // 13 // optimized trait classes for lossless compression of 8 bit color and 8/16 bit monochrome images. 14 // This class is assumes MAXVAL correspond to a whole number of bits, and no custom RESET value is set when encoding. 15 // The point of this is to have the most optimized code for the most common and most demanding scenario. 16 17 template <class sample, LONG bitsperpixel> 18 struct LosslessTraitsImplT 19 { 20 typedef sample SAMPLE; 21 enum { 22 NEAR = 0, 23 bpp = bitsperpixel, 24 qbpp = bitsperpixel, 25 RANGE = (1 << bpp), 26 MAXVAL= (1 << bpp) - 1, 27 LIMIT = 2 * (bitsperpixel + MAX(8,bitsperpixel)), 28 RESET = BASIC_RESET 29 }; 30 ComputeErrValLosslessTraitsImplT31 static inlinehint LONG ComputeErrVal(LONG d) 32 { return ModRange(d); } 33 IsNearLosslessTraitsImplT34 static inlinehint bool IsNear(LONG lhs, LONG rhs) 35 { return lhs == rhs; } 36 ModRangeLosslessTraitsImplT37 static inlinehint LONG ModRange(LONG Errval) 38 { 39 return LONG(Errval << (LONG_BITCOUNT - bpp)) >> (LONG_BITCOUNT - bpp); 40 } 41 ComputeReconstructedSampleLosslessTraitsImplT42 static inlinehint SAMPLE ComputeReconstructedSample(LONG Px, LONG ErrVal) 43 { 44 return SAMPLE(MAXVAL & (Px + ErrVal)); 45 } 46 CorrectPredictionLosslessTraitsImplT47 static inlinehint LONG CorrectPrediction(LONG Pxc) 48 { 49 if ((Pxc & MAXVAL) == Pxc) 50 return Pxc; 51 52 return (~(Pxc >> (LONG_BITCOUNT-1))) & MAXVAL; 53 } 54 55 }; 56 57 // For some weird reason MSVC6 doesn't like these templates. 58 // xlC (compiler on AIX) tries to instantiate Triple<Triple<char> > which 59 // causes compiler errors and is wrong (other compilers don't instantiate it). 60 #if (defined(_MSC_VER) && _MSC_VER <= 1200) || defined(__xlC__) 61 # define DISABLE_SPECIALIZATIONS 62 #else 63 64 template <class SAMPLE, LONG bpp> 65 struct LosslessTraitsT : public LosslessTraitsImplT<SAMPLE, bpp> 66 { 67 typedef SAMPLE PIXEL; 68 }; 69 70 71 72 template<> 73 struct LosslessTraitsT<BYTE,8> : public LosslessTraitsImplT<BYTE, 8> 74 { 75 typedef SAMPLE PIXEL; 76 77 static inlinehint signed char ModRange(LONG Errval) 78 { return (signed char)Errval; } 79 80 static inlinehint LONG ComputeErrVal(LONG d) 81 { return (signed char)(d); } 82 83 static inlinehint BYTE ComputeReconstructedSample(LONG Px, LONG ErrVal) 84 { return BYTE(Px + ErrVal); } 85 86 }; 87 88 89 90 template<> 91 struct LosslessTraitsT<USHORT,16> : public LosslessTraitsImplT<USHORT,16> 92 { 93 typedef SAMPLE PIXEL; 94 95 static inlinehint short ModRange(LONG Errval) 96 { return short(Errval); } 97 98 static inlinehint LONG ComputeErrVal(LONG d) 99 { return short(d); } 100 101 static inlinehint SAMPLE ComputeReconstructedSample(LONG Px, LONG ErrVal) 102 { return SAMPLE(Px + ErrVal); } 103 104 }; 105 106 107 108 109 template<class SAMPLE, LONG bpp> 110 struct LosslessTraitsT<Triplet<SAMPLE>,bpp> : public LosslessTraitsImplT<SAMPLE,bpp> 111 { 112 typedef Triplet<SAMPLE> PIXEL; 113 114 static inlinehint bool IsNear(LONG lhs, LONG rhs) 115 { return lhs == rhs; } 116 117 static inlinehint bool IsNear(PIXEL lhs, PIXEL rhs) 118 { return lhs == rhs; } 119 120 121 static inlinehint SAMPLE ComputeReconstructedSample(LONG Px, LONG ErrVal) 122 { return SAMPLE(Px + ErrVal); } 123 124 125 }; 126 #endif 127 128 #endif 129