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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/*************************************************************************
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/>.
*************************************************************************/
/*
** A JPEG LS scan covering only a single component.
**
** $Id: lineinterleavedlsscan.cpp,v 1.13 2014/11/14 15:41:32 thor Exp $
**
*/
/// Includes
#include "codestream/jpeglsscan.hpp"
#include "codestream/lineinterleavedlsscan.hpp"
///
/// LineInterleavedLSScan::LineInterleavedLSScan
// Create a new scan. This is only the base type.
LineInterleavedLSScan::LineInterleavedLSScan(class Frame *frame,class Scan *scan,
UBYTE near,const UBYTE *mapping,UBYTE point)
: JPEGLSScan(frame,scan,near,mapping,point)
{
}
///
/// LineInterleavedLSScan::~LineInterleavedLSScan
// Dispose a scan.
LineInterleavedLSScan::~LineInterleavedLSScan(void)
{
}
///
/// LineInterleavedLSScan::ParseMCU
// Parse a single MCU in this scan. Return true if there are more
// MCUs in this row.
bool LineInterleavedLSScan::ParseMCU(void)
{
#if ACCUSOFT_CODE
UBYTE cx;
UBYTE preshift = m_ucLowBit + FractionalColorBitsOf();
struct Line *line[4]; // Pointer to the lines.
UWORD mcuheight[4]; // number of lines in a MCU
ULONG ypos[4];
int lines = 8; // lines to process in this block of lines (not really a MCU)
bool more;
assert(m_ucCount < 4);
for(cx = 0;cx < m_ucCount;cx++) {
class Component *comp = ComponentOf(cx);
line[cx] = CurrentLine(cx);
mcuheight[cx] = comp->MCUHeightOf();
ypos[cx] = CurrentYOf(cx);
}
// Loop over lines, lines are coded independently, in groups where
// each group contains MCUheight lines of each.
do {
// MCU is a group of lines.
if (BeginReadMCU(m_Stream.ByteStreamOf())) {
//
// Loop over components.
for(cx = 0;cx < m_ucCount;cx++) {
UWORD ymcu;
// Loop over the MCU height of this component.
for(ymcu = 0;line[cx] && ymcu < mcuheight[cx];ymcu++) {
LONG length = m_ulWidth[cx];
LONG *lp = line[cx]->m_pData;
#ifdef DEBUG_LS
int xpos = 0;
static int linenumber = 0;
printf("\n%4d : ",++linenumber);
#endif
//
StartLine(cx);
do {
LONG a,b,c,d; // neighbouring values.
LONG d1,d2,d3; // local gradients.
GetContext(cx,a,b,c,d);
d1 = d - b; // compute local gradients
d2 = b - c;
d3 = c - a;
if (isRunMode(d1,d2,d3)) {
LONG run = DecodeRun(length,m_lRunIndex[cx]);
//
// Now fill the data.
while(run) {
// Update so that the next process gets the correct value.
UpdateContext(cx,a);
// And insert the value into the target line as well.
*lp++ = a << preshift;
#ifdef DEBUG_LS
printf("%4d:<%2x> ",xpos++,lp[-1]);
#endif
run--,length--;
// As long as there are pixels on the line.
}
//
// More data on the line? I.e. the run did not cover the full m_lJ samples?
// Now decode the run interruption sample.
if (length) {
bool negative; // the sign variable
bool rtype; // run interruption type
LONG errval; // the prediction error
LONG merr; // the mapped error (symbol)
LONG rx; // the reconstructed value
UBYTE k; // golomb parameter
// Get the neighbourhood.
GetContext(cx,a,b,c,d);
// Get the prediction mode.
rtype = InterruptedPredictionMode(negative,a,b);
// Get the golomb parameter for run interruption coding.
k = GolombParameter(rtype);
// Golomb-decode the error symbol.
merr = GolombDecode(k,m_lLimit - m_lJ[m_lRunIndex[cx]] - 1);
// Inverse the error mapping procedure.
errval = InverseErrorMapping(merr + rtype,ErrorMappingOffset(rtype,rtype || merr,k));
// Compute the reconstructed value.
rx = Reconstruct(negative,rtype?a:b,errval);
// Update so that the next process gets the correct value.
UpdateContext(cx,rx );
// Fill in the value into the line
*lp = rx << preshift;
#ifdef DEBUG_LS
printf("%4d:<%2x> ",xpos++,lp[0]);
#endif
// Update the variables of the run mode.
UpdateState(rtype,errval);
// Update the run index now. This is not part of
// EncodeRun because the non-reduced run-index is
// required for the golomb coder length limit.
if (m_lRunIndex[cx] > 0)
m_lRunIndex[cx]--;
} else break; // end of line.
} else {
UWORD ctxt;
bool negative; // the sign variable.
LONG px; // the predicted variable.
LONG rx; // the reconstructed value.
LONG errval; // the error value.
LONG merr; // the mapped error value.
UBYTE k; // the Golomb parameter.
// Quantize the gradients.
d1 = QuantizedGradient(d1);
d2 = QuantizedGradient(d2);
d3 = QuantizedGradient(d3);
// Compute the context.
ctxt = Context(negative,d1,d2,d3);
// Compute the predicted value.
px = Predict(a,b,c);
// Correct the prediction.
px = CorrectPrediction(ctxt,negative,px);
// Compute the golomb parameter k from the context.
k = GolombParameter(ctxt);
// Decode the error symbol.
merr = GolombDecode(k,m_lLimit);
// Inverse the error symbol into an error value.
errval = InverseErrorMapping(merr,ErrorMappingOffset(ctxt,k));
// Update the variables.
UpdateState(ctxt,errval);
// Compute the reconstructed value.
rx = Reconstruct(negative,px,errval);
// Update so that the next process gets the correct value.
UpdateContext(cx,rx);
// And insert the value into the target line as well.
*lp = rx << preshift;
#ifdef DEBUG_LS
printf("%4d:<%2x> ",xpos++,lp[0]);
#endif
}
} while(++lp,--length);
EndLine(cx);
line[cx] = line[cx]->m_pNext;
} // Of loop over MCU height
} // of loop over components.
} // Of MCU is correct
// Advance the Y pos over the MCU
for(more = true,cx = 0;cx < m_ucCount;cx++) {
ypos[cx] += mcuheight[cx];
if (m_ulHeight[cx] && ypos[cx] >= m_ulHeight[cx])
more = false;
}
} while(--lines && more);
// If this is the last line, gobble up all the
// bits from bitstuffing the last byte may have left.
// As SkipStuffing is idempotent, we can also do that
// all the time.
m_Stream.SkipStuffing();
#endif
return false;
}
///
/// LineInterleavedLSScan::WriteMCU
// Write a single MCU in this scan.
bool LineInterleavedLSScan::WriteMCU(void)
{
#if ACCUSOFT_CODE
UBYTE cx;
UBYTE preshift = m_ucLowBit + FractionalColorBitsOf();
struct Line *line[4]; // Pointer to the lines.
UWORD mcuheight[4]; // number of lines in a MCU
ULONG ypos[4];
int lines = 8; // lines to process in this block of lines (not really a MCU)
bool more;
assert(m_ucCount < 4);
for(cx = 0;cx < m_ucCount;cx++) {
class Component *comp = ComponentOf(cx);
line[cx] = CurrentLine(cx);
mcuheight[cx] = comp->MCUHeightOf();
ypos[cx] = CurrentYOf(cx);
}
// Loop over lines, lines are coded independently, in groups where
// each group contains MCUheight lines of each.
do {
// MCU is a group of lines.
BeginWriteMCU(m_Stream.ByteStreamOf());
//
// Loop over components.
for(cx = 0;cx < m_ucCount;cx++) {
UWORD ymcu;
// Loop over the MCU height of this component.
for(ymcu = 0;line[cx] && ymcu < mcuheight[cx];ymcu++) {
LONG length = m_ulWidth[cx];
LONG *lp = line[cx]->m_pData;
StartLine(cx);
do {
LONG a,b,c,d,x; // neighbouring values.
LONG d1,d2,d3; // local gradients.
GetContext(cx,a,b,c,d);
x = *lp >> preshift;
d1 = d - b; // compute local gradients
d2 = b - c;
d3 = c - a;
if (isRunMode(d1,d2,d3)) {
LONG runval = a;
LONG runcnt = 0;
do {
x = *lp >> preshift;
if (x - runval < -m_lNear || x - runval > m_lNear)
break;
// Update so that the next process gets the correct value.
// Also updates the line pointers.
UpdateContext(cx,runval);
} while(lp++,runcnt++,--length);
// Encode the run. Depends on whether the run was interrupted
// by the end of the line.
EncodeRun(runcnt,length == 0,m_lRunIndex[cx]);
// Continue the encoding of the end of the run if there are more
// samples to encode.
if (length) {
bool negative; // the sign variable
bool rtype; // run interruption type
LONG errval; // the prediction error
LONG merr; // the mapped error (symbol)
LONG rx; // the reconstructed value
UBYTE k; // golomb parameter
// Get the neighbourhood.
GetContext(cx,a,b,c,d);
// Get the prediction mode.
rtype = InterruptedPredictionMode(negative,a,b);
// Compute the error value.
errval = x - ((rtype)?(a):(b));
if (negative)
errval = -errval;
// Quantize the error.
errval = QuantizePredictionError(errval);
// Compute the reconstructed value.
rx = Reconstruct(negative,rtype?a:b,errval);
// Update so that the next process gets the correct value.
UpdateContext(cx,rx);
// Get the golomb parameter for run interruption coding.
k = GolombParameter(rtype);
// Map the error into a symbol.
merr = ErrorMapping(errval,ErrorMappingOffset(rtype,errval != 0,k)) - rtype;
// Golomb-coding of the error.
GolombCode(k,merr,m_lLimit - m_lJ[m_lRunIndex[cx]] - 1);
// Update the variables of the run mode.
UpdateState(rtype,errval);
// Update the run index now. This is not part of
// EncodeRun because the non-reduced run-index is
// required for the golomb coder length limit.
if (m_lRunIndex[cx] > 0)
m_lRunIndex[cx]--;
} else break; // Line ended, abort the loop over the line.
} else {
UWORD ctxt;
bool negative; // the sign variable.
LONG px; // the predicted variable.
LONG rx; // the reconstructed value.
LONG errval; // the error value.
LONG merr; // the mapped error value.
UBYTE k; // the Golomb parameter.
// Quantize the gradients.
d1 = QuantizedGradient(d1);
d2 = QuantizedGradient(d2);
d3 = QuantizedGradient(d3);
// Compute the context.
ctxt = Context(negative,d1,d2,d3);
// Compute the predicted value.
px = Predict(a,b,c);
// Correct the prediction.
px = CorrectPrediction(ctxt,negative,px);
// Compute the error value.
errval = x - px;
if (negative)
errval = -errval;
// Quantize the prediction error if NEAR > 0
errval = QuantizePredictionError(errval);
// Compute the reconstructed value.
rx = Reconstruct(negative,px,errval);
// Update so that the next process gets the correct value.
UpdateContext(cx,rx);
// Compute the golomb parameter k from the context.
k = GolombParameter(ctxt);
// Map the error into a symbol
merr = ErrorMapping(errval,ErrorMappingOffset(ctxt,k));
// Golomb-coding of the error.
GolombCode(k,merr,m_lLimit);
// Update the variables.
UpdateState(ctxt,errval);
}
} while(++lp,--length);
EndLine(cx);
// Go to the next line
line[cx] = line[cx]->m_pNext;
} // Of loop over MCU height
} // of loop over components.
// Advance the Y pos over the MCU
for(more = true,cx = 0;cx < m_ucCount;cx++) {
ypos[cx] += mcuheight[cx];
if (m_ulHeight[cx] && ypos[cx] >= m_ulHeight[cx])
more = false;
}
} while(--lines && more);
#endif
return false;
}
///
|