OFFIS DCMTK  Version 3.6.0
deftrait.h
1 //
2 // (C) Jan de Vaan 2007-2010, all rights reserved. See the accompanying "License.txt" for licensed use.
3 //
4 
5 
6 #ifndef CHARLS_DEFAULTTRAITS
7 #define CHARLS_DEFAULTTRAITS
8 
9 // Default traits that support all JPEG LS parameters: custom limit, near, maxval (not power of 2)
10 
11 // This traits class is used to initialize a coder/decoder.
12 // The coder/decoder also delegates some functions to the traits class.
13 // This is to allow the traits class to replace the default implementation here with optimized specific implementations.
14 // This is done for lossless coding/decoding: see losslesstraits.h
15 
16 template <class sample, class pixel>
18 {
19 public:
20  typedef sample SAMPLE;
21  typedef pixel PIXEL;
22 
23  LONG MAXVAL;
24  LONG RANGE;
25  LONG NEAR;
26  LONG qbpp;
27  LONG bpp;
28  LONG LIMIT;
29  LONG RESET;
30 
31  DefaultTraitsT(const DefaultTraitsT& src) :
32  MAXVAL(src.MAXVAL),
33  RANGE(src.RANGE),
34  NEAR(src.NEAR),
35  qbpp(src.qbpp),
36  bpp(src.bpp),
37  LIMIT(src.LIMIT),
38  RESET(src.RESET)
39  {
40  }
41 
42  DefaultTraitsT(LONG max, LONG jls_near)
43  {
44  NEAR = jls_near;
45  MAXVAL = max;
46  RANGE = (MAXVAL + 2 * NEAR )/(2 * NEAR + 1) + 1;
47  bpp = log_2(max);
48  LIMIT = 2 * (bpp + MAX(8,bpp));
49  qbpp = log_2(RANGE);
50  RESET = BASIC_RESET;
51  }
52 
53 
54  inlinehint LONG ComputeErrVal(LONG e) const
55  {
56  LONG q = Quantize(e);
57  return ModRange(q);
58  }
59 
60  inlinehint SAMPLE ComputeReconstructedSample(LONG Px, LONG ErrVal)
61  {
62  return FixReconstructedValue(Px + DeQuantize(ErrVal));
63  }
64 
65  inlinehint bool IsNear(LONG lhs, LONG rhs) const
66  { return ABS(lhs-rhs) <=NEAR; }
67 
68  bool IsNear(Triplet<SAMPLE> lhs, Triplet<SAMPLE> rhs) const
69  {
70  return ABS(lhs.v1-rhs.v1) <=NEAR &&
71  ABS(lhs.v2-rhs.v2) <=NEAR &&
72  ABS(lhs.v3-rhs.v3) <=NEAR;
73  }
74 
75  inlinehint LONG CorrectPrediction(LONG Pxc) const
76  {
77  if ((Pxc & MAXVAL) == Pxc)
78  return Pxc;
79 
80  return (~(Pxc >> (LONG_BITCOUNT-1))) & MAXVAL;
81  }
82 
83  inlinehint LONG ModRange(LONG Errval) const
84  {
85  ASSERT(ABS(Errval) <= RANGE);
86  if (Errval < 0)
87  Errval = Errval + RANGE;
88 
89  if (Errval >= ((RANGE + 1) / 2))
90  Errval = Errval - RANGE;
91 
92  ASSERT(ABS(Errval) <= RANGE/2);
93 
94  return Errval;
95  }
96 
97 
98 private:
99  LONG Quantize(LONG Errval) const
100  {
101  if (Errval > 0)
102  return (Errval + NEAR) / (2 * NEAR + 1);
103  else
104  return - (NEAR - Errval) / (2 * NEAR + 1);
105  }
106 
107 
108  inlinehint LONG DeQuantize(LONG Errval) const
109  {
110  return Errval * (2 * NEAR + 1);
111  }
112 
113  inlinehint SAMPLE FixReconstructedValue(LONG val) const
114  {
115  if (val < -NEAR)
116  val = val + RANGE*(2*NEAR+1);
117  else if (val > MAXVAL + NEAR)
118  val = val - RANGE*(2*NEAR+1);
119 
120  return SAMPLE(CorrectPrediction(val));
121  }
122 
123 };
124 
125 
126 #endif


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2