-
Notifications
You must be signed in to change notification settings - Fork 2
/
jpeg_ls_model.h
358 lines (281 loc) · 9.08 KB
/
jpeg_ls_model.h
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#ifndef JPEG_LS_MODEL_H
#define JPEG_LS_MODEL_H
#include <vector>
using namespace std;
#include "common.h"
#include "model.h"
#include "arithmetic_encoder.h"
#include "arithmetic_decoder.h"
#include "index.h"
#include "adaptive_model.h"
#include "bitmap.h"
#include <algorithm>
using namespace std;
class jpeg_ls_model : public adaptive_model<BYTE>
{
private:
BYTE m_PredictedValue;
bitmap8bpp* m_Bmp;
jpeg_ls_model(const jpeg_ls_model&);
jpeg_ls_model& operator=(const jpeg_ls_model&);
public:
jpeg_ls_model(arithmetic_encoder* encoder, bitmap8bpp* bmp);
jpeg_ls_model(arithmetic_decoder* decoder, bitmap8bpp* bmp);
void predict5(DWORD i, DWORD j);
virtual void encode_symbol(BYTE symbol);
virtual void encode_eof();
virtual BYTE decode_symbol();
virtual ~jpeg_ls_model()
{
}
};
///////////////////////////////////////////////////////////
jpeg_ls_model::jpeg_ls_model(arithmetic_encoder* encoder, bitmap8bpp* bmp) :
adaptive_model<BYTE>(encoder), m_Bmp(bmp)
{
m_PredictedValue = 0;
}
jpeg_ls_model::jpeg_ls_model(arithmetic_decoder* decoder, bitmap8bpp* bmp) :
adaptive_model<BYTE>(decoder), m_Bmp(bmp)
{
m_PredictedValue = 0;
}
void jpeg_ls_model::predict5(DWORD i, DWORD j)
{
BYTE N = m_Bmp->get_greyed_pixel(i - 1, j);
BYTE W = m_Bmp->get_greyed_pixel(i, j - 1);
BYTE NW = m_Bmp->get_greyed_pixel(i - 1, j - 1);
if (!(NW < _MAX(W, N)))
{
m_PredictedValue = _MAX(W, N);
}
else
{
if (!(NW > _MIN(W, N)))
m_PredictedValue = _MIN(W, N);
else
m_PredictedValue = W + N - NW;
}
}
void jpeg_ls_model::encode_symbol(BYTE symbol)
{
if (m_encoder)
{
BYTE cbDistance = symbol - m_PredictedValue;
DWORD iDistance = idx<BYTE>::from_symbol(cbDistance);
m_encoder->encode(cum_count(iDistance - 1),
cum_count(iDistance),
total_count());
update(cbDistance);
}
}
void jpeg_ls_model::encode_eof()
{
m_encoder->encode_eof();
}
BYTE jpeg_ls_model::decode_symbol()
{
BYTE symbol;
if (m_decoder)
{
DWORD target = m_decoder->decode_target(total_count());
DWORD index = search_index(target);
m_decoder->decode(cum_count(index - 1),
cum_count(index),
total_count());
BYTE cbDistance = idx<BYTE>::to_symbol(index);
symbol = m_PredictedValue + cbDistance;
update(cbDistance);
}
return symbol;
}
//////////////////////////// The model for true color BMPs ///////////////////////////////////
class jpeg_ls_24_model : public model<rgb_color24>
{
private:
rgb_color24 m_rgbPredictedValue;
bitmap_true<rgb_color24>* m_Bmp;
enum { dwSize = 257 };
DWORD m_aRedCumCount [dwSize];
DWORD m_aGreenCumCount[dwSize];
DWORD m_aBlueCumCount [dwSize];
//DWORD m_aAlphaCumCount[dwSize];
arithmetic_encoder* m_encoder;
arithmetic_decoder* m_decoder;
jpeg_ls_24_model(const jpeg_ls_24_model&);
jpeg_ls_24_model& operator=(const jpeg_ls_24_model&);
void init(DWORD aColorCumCount[]);
DWORD cum_count(size_t iSymbol, DWORD aColorCumCount[]);
DWORD search_index(DWORD dwTarget, DWORD aColorCumCount[]);
DWORD total_count(DWORD aColorCumCount[]);
void update(BYTE bColorChannel, DWORD aColorCumCount[]);
void update(rgb_color24 rgbNewColor);
void encode_channel(DWORD iColorDistance, DWORD aColorCumCount[]);
public:
jpeg_ls_24_model(arithmetic_encoder* encoder, bitmap_true<rgb_color24>* bmp);
jpeg_ls_24_model(arithmetic_decoder* decoder, bitmap_true<rgb_color24>* bmp);
void predict5(DWORD i, DWORD j);
virtual void encode_symbol(rgb_color24 symbol);
virtual void encode_eof();
virtual rgb_color24 decode_symbol();
virtual ~jpeg_ls_24_model()
{
}
};
////////////////////////////// ctor /////////////////////////////
jpeg_ls_24_model::jpeg_ls_24_model(arithmetic_encoder* encoder, bitmap_true<rgb_color24>* bmp) :
m_Bmp(bmp),
m_encoder(encoder),
m_decoder(NULL)
{
init(m_aRedCumCount);
init(m_aGreenCumCount);
init(m_aBlueCumCount);
//init(m_aAlphaCumCount);
}
jpeg_ls_24_model::jpeg_ls_24_model(arithmetic_decoder* decoder, bitmap_true<rgb_color24>* bmp) :
m_Bmp(bmp),
m_encoder(NULL),
m_decoder(decoder)
{
init(m_aRedCumCount);
init(m_aGreenCumCount);
init(m_aBlueCumCount);
//init(m_aAlphaCumCount);
}
////////////////////////// private //////////////////////////////
void jpeg_ls_24_model::init(DWORD aColorCumCount[])
{
aColorCumCount[0] = 0;
for (DWORD i = 1; i < dwSize; ++i)
aColorCumCount[i] = i;
}
inline DWORD jpeg_ls_24_model::cum_count(size_t iSymbol, DWORD aColorCumCount[])
{
return(aColorCumCount[iSymbol]);
}
inline DWORD jpeg_ls_24_model::search_index(DWORD dwTarget, DWORD aColorCumCount[])
{
register DWORD k;
for (k = 1; k < dwSize; ++k)
if ((aColorCumCount[k - 1] <= dwTarget) && (dwTarget < aColorCumCount[k]))
break;
return k;
}
inline DWORD jpeg_ls_24_model::total_count(DWORD aColorCumCount[])
{
ASSERT(aColorCumCount[dwSize - 1] > aColorCumCount[dwSize - 2]);
return(aColorCumCount[dwSize - 1]);
}
void jpeg_ls_24_model::update(BYTE bColorChannel, DWORD aColorCumCount[])
{
for (DWORD j = idx<BYTE>::from_symbol(bColorChannel); j < dwSize; ++j)
{
++(aColorCumCount[j]);
}
if (aColorCumCount[dwSize - 1] >= ((DWORD)UPPER_LIMIT >> 2) )
{
for (DWORD i = 1; i < dwSize; ++i)
{
aColorCumCount[i] >>= 1;
while (aColorCumCount[i] <= aColorCumCount[i - 1])
++aColorCumCount[i];
}
}
}
void jpeg_ls_24_model::update(rgb_color24 rgbNewColor)
{
update(rgbNewColor.rgbRed, m_aRedCumCount );
update(rgbNewColor.rgbGreen, m_aGreenCumCount);
update(rgbNewColor.rgbBlue, m_aBlueCumCount );
//update(rgbNewColor.rgbReserved, m_aAlphaCumCount);
}
void jpeg_ls_24_model::predict5(DWORD i, DWORD j)
{
rgb_color24 rgbN = m_Bmp->get_pixel(i - 1, j);
rgb_color24 rgbW = m_Bmp->get_pixel(i, j - 1);
rgb_color24 rgbNW = m_Bmp->get_pixel(i - 1, j - 1);
if (!(rgbNW < _MAX(rgbW, rgbN)))
{
m_rgbPredictedValue = _MAX(rgbW, rgbN);
}
else
{
if (!(rgbNW > _MIN(rgbW, rgbN)))
m_rgbPredictedValue = _MIN(rgbW, rgbN);
else
m_rgbPredictedValue = rgbW + rgbN - rgbNW;
}
}
void jpeg_ls_24_model::encode_channel(DWORD iColorDistance, DWORD aColorCumCount[])
{
m_encoder->encode(cum_count(iColorDistance - 1, aColorCumCount),
cum_count(iColorDistance, aColorCumCount),
total_count(aColorCumCount));
}
/////////////////////////// publics ////////////////////////////////////
void jpeg_ls_24_model::encode_symbol(rgb_color24 rgbSymbol)
{
if (m_encoder)
{
DWORD iDistance;
rgb_color24 rgbDistance = rgbSymbol - m_rgbPredictedValue;
iDistance = idx<BYTE>::from_symbol(rgbDistance.rgbRed);
encode_channel(iDistance, m_aRedCumCount);
iDistance = idx<BYTE>::from_symbol(rgbDistance.rgbGreen);
encode_channel(iDistance, m_aGreenCumCount);
iDistance = idx<BYTE>::from_symbol(rgbDistance.rgbBlue);
encode_channel(iDistance, m_aBlueCumCount);
//iDistance = idx<BYTE>::from_symbol(rgbDistance.rgbReserved);
//encode_channel(iDistance, m_aAlphaCumCount);
update(rgbDistance);
}
}
void jpeg_ls_24_model::encode_eof()
{
m_encoder->encode_eof();
}
rgb_color24 jpeg_ls_24_model::decode_symbol()
{
rgb_color24 rgbSymbol;
if (m_decoder)
{
rgb_color24 rgbDistance;
//red
DWORD dwRedTarget = m_decoder->decode_target(total_count(m_aRedCumCount));
DWORD iRed = search_index(dwRedTarget, m_aRedCumCount);
m_decoder->decode(cum_count(iRed - 1, m_aRedCumCount),
cum_count(iRed, m_aRedCumCount),
total_count(m_aRedCumCount));
BYTE cbRedDistance = idx<BYTE>::to_symbol(iRed);
rgbDistance.rgbRed = cbRedDistance;
//green
DWORD dwGreenTarget = m_decoder->decode_target(total_count(m_aGreenCumCount));
DWORD iGreen = search_index(dwGreenTarget, m_aGreenCumCount);
m_decoder->decode(cum_count(iGreen - 1, m_aGreenCumCount),
cum_count(iGreen, m_aGreenCumCount),
total_count(m_aGreenCumCount));
BYTE cbGreenDistance = idx<BYTE>::to_symbol(iGreen);
rgbDistance.rgbGreen = cbGreenDistance;
//blue
DWORD dwBlueTarget = m_decoder->decode_target(total_count(m_aBlueCumCount));
DWORD iBlue = search_index(dwBlueTarget, m_aBlueCumCount);
m_decoder->decode(cum_count(iBlue - 1, m_aBlueCumCount),
cum_count(iBlue, m_aBlueCumCount),
total_count(m_aBlueCumCount));
BYTE cbBlueDistance = idx<BYTE>::to_symbol(iBlue);
rgbDistance.rgbBlue = cbBlueDistance;
//alpha
/*DWORD dwAlphaTarget = m_decoder->decode_target(total_count(m_aAlphaCumCount));
DWORD iAlpha = search_index(dwAlphaTarget, m_aAlphaCumCount);
m_decoder->decode(cum_count(iAlpha - 1, m_aAlphaCumCount),
cum_count(iAlpha, m_aAlphaCumCount),
total_count(m_aAlphaCumCount));
BYTE cbAlphaDistance = idx<BYTE>::to_symbol(iAlpha);
rgbDistance.rgbReserved = cbAlphaDistance;*/
rgbSymbol = m_rgbPredictedValue + rgbDistance;
update(rgbDistance);
}
return rgbSymbol;
}
#endif //JPEG_LS_MODEL_H