File: aclosslessscan.hpp

package info (click to toggle)
libjpeg 0.0~git20241105.c719010-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,904 kB
  • sloc: cpp: 36,434; makefile: 618; ansic: 275; sh: 54; python: 39; perl: 11
file content (237 lines) | stat: -rw-r--r-- 7,173 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*************************************************************************

    This project implements a complete(!) JPEG (Recommendation ITU-T
    T.81 | ISO/IEC 10918-1) codec, plus a library that can be used to
    encode and decode JPEG streams. 
    It also implements ISO/IEC 18477 aka JPEG XT which is an extension
    towards intermediate, high-dynamic-range lossy and lossless coding
    of JPEG. In specific, it supports ISO/IEC 18477-3/-6/-7/-8 encoding.

    Note that only Profiles C and D of ISO/IEC 18477-7 are supported
    here. Check the JPEG XT reference software for a full implementation
    of ISO/IEC 18477-7.

    Copyright (C) 2012-2018 Thomas Richter, University of Stuttgart and
    Accusoft. (C) 2019-2020 Thomas Richter, Fraunhofer IIS.

    This program is available under two licenses, GPLv3 and the ITU
    Software licence Annex A Option 2, RAND conditions.

    For the full text of the GPU license option, see README.license.gpl.
    For the full text of the ITU license option, see README.license.itu.
    
    You may freely select between these two options.

    For the GPL option, please note the following:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*************************************************************************/
/*
**
** Represents the lossless scan - lines are coded directly with predictive
** coding, though here residuals are encoded with the arithmetic encoder.
**
** $Id: aclosslessscan.hpp,v 1.29 2022/05/23 05:56:51 thor Exp $
**
*/

#ifndef CODESTREAM_ACLOSSLESSSCAN_HPP
#define CODESTREAM_ACLOSSLESSSCAN_HPP

/// Includes
#include "tools/environment.hpp"
#include "coding/qmcoder.hpp"
#include "codestream/entropyparser.hpp"
#include "codestream/predictivescan.hpp"
///

/// Forwards
class Frame;
class LineCtrl;
class ByteStream;
class LineBitmapRequester;
class LineBuffer;
class BitmaCtrl;
class Scan;
///

/// class LosslessScan
// Represents the lossless scan - lines are coded directly with predictive
// coding, though here residuals are encoded with the arithmetic encoder.
class ACLosslessScan : public PredictiveScan {
  //
#if ACCUSOFT_CODE
  // The class used for pulling and pushing data.
  class LineBuffer          *m_pLineCtrl;
  //
  // Small DC threshold value ('L' in the standard)
  UBYTE                      m_ucSmall[4];
  //
  // Large DC threshold value ('U' in the specs)
  UBYTE                      m_ucLarge[4];
  //
  // The context index to use.
  UBYTE                      m_ucContext[4];
  //
  // Differentials from the above and left, used
  // for prediction.
  LONG                      *m_plDa[4];
  LONG                      *m_plDb[4];
  //
  // The real worker class.
  class QMCoder              m_Coder;
  //
  // Context information.
  struct QMContextSet {
    //
    // The Zero-Sign coding contexts - this is a 5x5 set.
    struct ContextZeroSet {
      QMContext S0,SS,SP,SN;
      //
      void Init(void)
      {
        S0.Init();
        SS.Init();
        SP.Init();
        SN.Init();
      }
    } SignZeroCoding[5][5];
    //
    // The Magnitude/refinement coding contexts.
    struct MagnitudeSet {
      enum {
        MagnitudeContexts = 15
      };
      //
      QMContext X[MagnitudeContexts];
      QMContext M[MagnitudeContexts];
      //
      void Init(void)
      {
        for(int i = 0;i < MagnitudeContexts;i++) {
          X[i].Init();
          M[i].Init();
        }
      }
    } MagnitudeLow,MagnitudeHigh;
    //
    void Init(void)
    {
      for(int i = 0;i <5;i++) {
        for(int j = 0;j < 5;j++) {
          SignZeroCoding[i][j].Init();
        }
      }
      MagnitudeLow.Init();
      MagnitudeHigh.Init();
    }
    //
    // Classify and return the sign/zero coding context to encode the difference in.
    // Requires the differences in both directions.
    struct ContextZeroSet &ClassifySignZero(LONG Da,LONG Db,UBYTE l,UBYTE u)
    {
      return SignZeroCoding[Classify(Da,l,u) + 2][Classify(Db,l,u) + 2];
    }
    //
    // Classify the Magnitude context 
    struct MagnitudeSet &ClassifyMagnitude(LONG Db,UBYTE u)
    {
      if (Db > (1 << u) || -Db > (1 << u)) {
        return MagnitudeHigh;
      } else {
        return MagnitudeLow;
      }
    }
    //
    // Classifier in one direction.
    static int Classify(LONG diff,UBYTE l,UBYTE u)
    {
      LONG abs = (diff > 0)?(diff):(-diff);
  
      if (abs <= ((1 << l) >> 1)) {
        // the zero cathegory.
        return 0;
      }
      if (abs <= (1 << u)) {
        if (diff < 0) {
          return -1;
        } else {
          return 1;
        }
      }
      if (diff < 0) {
        return -2;
      } else {
        return 2;
      }
    }
    //
  } m_Context[4];
  //
  // Common setup for encoding and decoding.
  void FindComponentDimensions(void);
  //
#endif
  // This is actually the true MCU-parser, not the interface that reads
  // a full line.
  void ParseMCU(struct Line **prev,struct Line **top);
  //
  // The actual MCU-writer, write a single group of pixels to the stream,
  // or measure their statistics.
  void WriteMCU(struct Line **prev,struct Line **top);
  //
  // Flush the remaining bits out to the stream on writing.
  virtual void Flush(bool final); 
  // 
  // Restart the parser at the next restart interval
  virtual void Restart(void);
  //
  //
public:
  ACLosslessScan(class Frame *frame,class Scan *scan,UBYTE predictor,UBYTE lobit,
                 bool differential = false);
  //
  virtual ~ACLosslessScan(void);
  //
  // Write the marker that indicates the frame type fitting to this scan.
  virtual void WriteFrameType(class ByteStream *io);
  //
  // Fill in the tables for decoding and decoding parameters in general.
  virtual void StartParseScan(class ByteStream *io,class Checksum *chk,class BufferCtrl *ctrl);
  //
  // Write the default tables for encoding
  virtual void StartWriteScan(class ByteStream *io,class Checksum *chk,class BufferCtrl *ctrl);
  //
  // Start the measurement run - not required here.
  virtual void StartMeasureScan(class BufferCtrl *ctrl);
  //
  // Start a MCU scan. Returns true if there are more rows. False otherwise.
  // Note that we emulate here that MCUs are multiples of eight lines high
  // even though from a JPEG perspective a MCU is a single pixel in the
  // lossless coding case.
  virtual bool StartMCURow(void);
  //
  // Parse a single MCU in this scan. Return true if there are more
  // MCUs in this row.
  virtual bool ParseMCU(void);
  //
  // Write a single MCU in this scan.
  virtual bool WriteMCU(void);  
};
///


///
#endif