File: linemerger.cpp

package info (click to toggle)
libjpeg 0.0~git20210129.91985dc-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,664 kB
  • sloc: cpp: 36,104; makefile: 613; ansic: 275; sh: 54; python: 38; perl: 11
file content (670 lines) | stat: -rw-r--r-- 21,464 bytes parent folder | download
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*************************************************************************

    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/>.

*************************************************************************/
/*
**
** This class merges the two sources of a differential frame together,
** expanding its non-differential source.
**
** $Id: linemerger.cpp,v 1.27 2015/06/03 15:37:25 thor Exp $
**
*/

/// Includes
#include "tools/environment.hpp"
#include "tools/line.hpp"
#include "control/lineadapter.hpp"
#include "control/linemerger.hpp"
#include "marker/frame.hpp"
#include "marker/component.hpp"
#include "codestream/tables.hpp"
#include "colortrafo/colortrafo.hpp"
#include "std/string.hpp"
#define COLOR_BITS ColorTrafo::COLOR_BITS
///

/// LineMerger::LineMerger
// The frame to create the line merger from is the highpass frame as
// its line dimensions are identical to that of the required output.
LineMerger::LineMerger(class Frame *frame,class LineAdapter *low,class LineAdapter *high,
                       bool expandhor,bool expandver)
  : LineAdapter(frame)
#if ACCUSOFT_CODE
  , m_pFrame(frame), 
    m_pLowPass(low), m_pHighPass(high), 
    m_ppVBuffer(NULL), m_ppHBuffer(NULL), m_ppIBuffer(NULL),
    m_ppFirstLine(NULL), m_pppImage(NULL),
    m_ppTop(NULL), m_ppCenter(NULL), m_ppBottom(NULL),
    m_pulPixelWidth(NULL), m_pulPixelHeight(NULL), m_pulY(NULL),
    m_bExpandH(expandhor), m_bExpandV(expandver)
#endif
{
#if ACCUSOFT_CODE
#else
  NOREF(frame);
  NOREF(low);
  NOREF(high);
  NOREF(expandhor);
  NOREF(expandver);
#endif
}
///

/// LineMerger::~LineMerger
LineMerger::~LineMerger(void)
{
#if ACCUSOFT_CODE
  UBYTE i;
  
  if (m_ppVBuffer) {
    for(i = 0;i < m_ucCount;i++) {
      FreeLine(m_ppVBuffer[i],i);
    }
    m_pEnviron->FreeMem(m_ppVBuffer,m_ucCount * sizeof(struct Line *));
  }  

  if (m_ppHBuffer) {
    for(i = 0;i < m_ucCount;i++) {
      FreeLine(m_ppHBuffer[i],i);
    }
    m_pEnviron->FreeMem(m_ppHBuffer,m_ucCount * sizeof(struct Line *));
  }
  
  if (m_ppIBuffer) {
    for(i = 0;i < m_ucCount;i++) {
      FreeLine(m_ppIBuffer[i],i);
    }
    m_pEnviron->FreeMem(m_ppIBuffer,m_ucCount * sizeof(struct Line *));
  }

  if (m_ppFirstLine) {
    for(i = 0;i < m_ucCount;i++) {
      struct Line *line;
      while((line = m_ppFirstLine[i])) {
        m_ppFirstLine[i] = line->m_pNext;
        FreeLine(line,i);
      }
    }
    m_pEnviron->FreeMem(m_ppFirstLine,m_ucCount * sizeof(struct Line *));
  }
  
  if (m_pppImage) {
    m_pEnviron->FreeMem(m_pppImage,m_ucCount * sizeof(struct Line **));
  }

  //
  // Top, center and bottom are all part of the image and
  // hence released there.
  if (m_ppTop) {
    m_pEnviron->FreeMem(m_ppTop,m_ucCount * sizeof(struct Line *));
  }
  
  if (m_ppCenter) {
    m_pEnviron->FreeMem(m_ppCenter,m_ucCount * sizeof(struct Line *));
  }
  
  if (m_ppBottom) {
    m_pEnviron->FreeMem(m_ppBottom,m_ucCount * sizeof(struct Line *));
  }
  
  if (m_pulY) {
    m_pEnviron->FreeMem(m_pulY,m_ucCount * sizeof(ULONG));
  }

  if (m_pulPixelWidth) {
    m_pEnviron->FreeMem(m_pulPixelWidth,m_ucCount * sizeof(ULONG));
  }

  if (m_pulPixelHeight) {
    m_pEnviron->FreeMem(m_pulPixelHeight,m_ucCount * sizeof(ULONG));
  }
#endif
}
///

/// LineMerger::BuildCommon
// Second-stage constructor, construct the internal details.
void LineMerger::BuildCommon(void)
{
#if ACCUSOFT_CODE
  UBYTE i;

  LineAdapter::BuildCommon();

  if (m_ppVBuffer == NULL) {
    m_ppVBuffer = (struct Line **)m_pEnviron->AllocMem(m_ucCount * sizeof(struct Line *));
    memset(m_ppVBuffer,0,sizeof(struct Line *) * m_ucCount);
  }
  
  if (m_ppHBuffer == NULL) {
    m_ppHBuffer = (struct Line **)m_pEnviron->AllocMem(m_ucCount * sizeof(struct Line *));
    memset(m_ppHBuffer,0,sizeof(struct Line *) * m_ucCount);
  }
  
  if (m_ppIBuffer == NULL) {
    m_ppIBuffer = (struct Line **)m_pEnviron->AllocMem(m_ucCount * sizeof(struct Line *));
    memset(m_ppIBuffer,0,sizeof(struct Line *) * m_ucCount);
  }

  if (m_pppImage == NULL) {
    m_pppImage = (struct Line ***)m_pEnviron->AllocMem(m_ucCount * sizeof(struct Line **));
  }

  if (m_ppTop == NULL) {
    m_ppTop = (struct Line **)m_pEnviron->AllocMem(m_ucCount * sizeof(struct Line **));
    memset(m_ppTop,0,sizeof(struct Line *) * m_ucCount);
  }

  if (m_ppCenter == NULL) {
    m_ppCenter = (struct Line **)m_pEnviron->AllocMem(m_ucCount * sizeof(struct Line **));
    memset(m_ppCenter,0,sizeof(struct Line *) * m_ucCount);
  }

  if (m_ppBottom == NULL) {
    m_ppBottom = (struct Line **)m_pEnviron->AllocMem(m_ucCount * sizeof(struct Line **));
    memset(m_ppBottom,0,sizeof(struct Line *) * m_ucCount);
  }
  
  if (m_ppFirstLine == NULL) {
    m_ppFirstLine = (struct Line **)m_pEnviron->AllocMem(m_ucCount * sizeof(struct Line **));
    memset(m_ppFirstLine,0,sizeof(struct Line *) * m_ucCount);
  }
  
  if (m_pulY == NULL) {
    m_pulY = (ULONG *)m_pEnviron->AllocMem(m_ucCount * sizeof(ULONG));
  }
  
  if (m_pulPixelWidth == NULL) {
    ULONG w = m_pFrame->WidthOf();
    ULONG h = m_pFrame->HeightOf();
    
    assert(m_pulPixelHeight == NULL);

    m_pulPixelWidth  = (ULONG *)m_pEnviron->AllocMem(m_ucCount * sizeof(ULONG));
    m_pulPixelHeight = (ULONG *)m_pEnviron->AllocMem(m_ucCount * sizeof(ULONG));
    
    for(i = 0;i < m_ucCount;i++) {
      class Component *comp = m_pFrame->ComponentOf(i);
      UBYTE subx            = comp->SubXOf();
      UBYTE suby            = comp->SubYOf();
      m_pulPixelWidth[i]    = (w + subx - 1) / subx;
      m_pulPixelHeight[i]   = (h + suby - 1) / suby;
      m_pppImage[i]         = m_ppFirstLine + i;
      m_pulY[i]             = 0;
    }
  }
#endif
}
///


/// LineMerger::GetNextLowPassLine
// Fetch the next line from the low-pass and expand it horizontally if required.
#if ACCUSOFT_CODE
struct Line *LineMerger::GetNextLowpassLine(UBYTE comp)
{
  struct Line *line,*xline = AllocLine(comp);
  assert(m_ppHBuffer[comp] == NULL);
  m_ppHBuffer[comp]        = xline;
  //
  line                     = m_pLowPass->GetNextLine(comp);
  //
  // If horizontal expansion is required, do this now.
  if (m_bExpandH) {
    // Replicate the last pixel to make things simpler. There is enough storage allocated.
    // The destionation gets potentially one line longer.
    LONG *src  = line->m_pData;
    LONG *last = line->m_pData + ((m_pulPixelWidth[comp] + 1) >> 1);
    LONG *dst  = xline->m_pData;
    last[0]    = last[-1];
    do {
      *dst++   =  src[0];
      *dst++   = (src[0] + src[1]) >> 1;
    } while(++src < last);
  } else {
    memcpy(xline->m_pData,line->m_pData,m_pulPixelWidth[comp] * sizeof(LONG));
  }
  //
  m_pLowPass->ReleaseLine(line,comp);

  return xline;
}
#endif
///

/// LineMerger::GetNextExpandedLowPassLine
// Fetch a line from the low-pass filter and expand it in horizontal
// or vertical direction. Do not do anything else.
#if ACCUSOFT_CODE
struct Line *LineMerger::GetNextExpandedLowPassLine(UBYTE comp)
{
  // If vertical expansion is required, a new line is required from the lowpass:
  // 1) for the first line to have a reference,
  // 2) then on every odd line.
  if (m_bExpandV) {
    struct Line *line;
    // Fetch a next line on the first round, or on odd lines. The last
    // line of the image is replicated.
    if (m_pulY[comp] == 0 ||        // first line
        ((m_pulY[comp] & 1) &&      // or odd
         (m_pulPixelHeight[comp] == 0 ||  // and not last line
          ((m_pulY[comp] + 1) >> 1) < ((m_pulPixelHeight[comp] + 1) >> 1)))) {
      line = GetNextLowpassLine(comp);
      //
      // Buffer for the next line in between if this is the first line.
      if (m_pulY[comp] == 0) {
        assert(m_ppVBuffer[comp] == NULL);
        assert(m_ppHBuffer[comp] == line);
        m_ppVBuffer[comp] = line;
        m_ppHBuffer[comp] = NULL;
      }
    } else {
      // The buffered line will do.
      line = m_ppVBuffer[comp];
    }
    //
    // Line contains now the new line to expand. If we are currently an
    // even line, then this is all what is required. If we are an odd
    // line, this line must be merged with the previous line to get the final
    if (m_pulY[comp] & 1) {
      struct Line *out  = m_ppIBuffer[comp]?m_ppIBuffer[comp]:AllocLine(comp);
      struct Line *prev = m_ppVBuffer[comp];
      struct Line *next = line;
      LONG *dst         = out->m_pData;
      LONG *end         = out->m_pData + m_pulPixelWidth[comp];
      LONG *src1        = prev->m_pData;
      LONG *src2        = next->m_pData;
      // Odd line. Final output is composed of: This line (which is the next) 
      // merged with the previous plus the difference.
      do {
        *dst = (*src1++ + *src2++) >> 1;
      } while(++dst < end);
      //
      // Release the old buffered line which is no longer required
      // and store the new one there. This releases the old VBuffer.
      m_ppHBuffer[comp]   = NULL;
      if (prev != next) {
        FreeLine(prev,comp);
        m_ppVBuffer[comp] = next;
      }
      m_ppIBuffer[comp]   = out;
      //
      m_pulY[comp]++;
      return out;
    } else {
      // No interpolation necessary, data comes directly from the output buffer.
      m_pulY[comp]++;
      assert(m_ppVBuffer[comp] == line);
      return line;
    }
  } else {
    struct Line *line;
    line = GetNextLowpassLine(comp);
    //
    // For consistency, place into the VBuffer.
    assert(line == m_ppHBuffer[comp]);
    if (m_ppVBuffer[comp]) FreeLine(m_ppVBuffer[comp],comp);
    m_ppVBuffer[comp] = line;
    m_ppHBuffer[comp] = NULL;
    return line;
  }
}
#endif
///

/// LineMerger::GetNextLine
// Get the next available line from the output
// buffer on reconstruction. The caller must make
// sure that the buffer is really loaded up to the
// point or the line will be neutral grey.
struct Line *LineMerger::GetNextLine(UBYTE comp)
{
#if ACCUSOFT_CODE
  struct Line *low  = GetNextExpandedLowPassLine(comp);
  struct Line *high = m_pHighPass->GetNextLine(comp);
  LONG shift        = m_pHighPass->DCOffsetOf();
  //
  // Add up the lines.
  LONG *src = low->m_pData;
  LONG *dst = high->m_pData;
  LONG *end = high->m_pData + m_pulPixelWidth[comp];
  if (m_pHighPass->isLossless()) {
    do {
      *dst = ((*dst >> COLOR_BITS) + ((*src++ - shift) >> COLOR_BITS)) << COLOR_BITS;
      //*dst++ = *src++; // low-pass only
      //dst++;           // high-pass only
    } while(++dst < end);
  } else {
    do {
      *dst += *src++ - shift; // the DCT always adds the level shift, so subtract it.
      //*dst = *src++; // low-pass only
      //dst++;           // high-pass only
    } while(++dst < end);
  }

  return high;
#else
  NOREF(comp);
  return NULL;
#endif
}
///

/// LineMerger::ReleaseLine
// Release a line passed out to the user. As the lines always come from the highpass,
// let the highpass release it.
void LineMerger::ReleaseLine(struct Line *line,UBYTE comp)
{
#if ACCUSOFT_CODE
  m_pHighPass->ReleaseLine(line,comp);
#else
  NOREF(line);
  NOREF(comp);
#endif
}
///

/// LineMerger::AllocateLine
// Allocate a new line for encoding.
struct Line *LineMerger::AllocateLine(UBYTE comp)
{
#if ACCUSOFT_CODE
  struct Line *line;
  //
  // As we release ourselves...
  line              = AllocLine(comp);
  *m_pppImage[comp] = line;
  m_pppImage[comp]  = &(line->m_pNext);

  return line;
#else
  NOREF(comp);
  return NULL;
#endif
}
///

/// LineMerger::PushLine
// Push the next line into the output buffer. If eight lines
// are accumulated (or enough lines up to the end of the image)
// these lines are automatically transfered to the input
// buffer of the block based coding back-end.
void LineMerger::PushLine(struct Line *line,UBYTE comp)
{
#if ACCUSOFT_CODE
  if (m_bExpandV) {
    // A new differential line can be computed on odd lines, and on the last line of the image.
    // For odd lines, the line before, and the line before that line are required.
    if (m_pulY[comp] & 1) {
      // Place the next row into the bottom buffer, its old content goes into top.
      assert(m_ppTop[comp] == NULL);
      m_ppTop[comp]        = m_ppBottom[comp];
      m_ppBottom[comp]     = line;
      //
      struct Line *center  = m_ppCenter[comp];
      struct Line *bottom  = line;
      struct Line *top     = m_ppTop[comp];
      struct Line *out;
      //
      // Allocate a temporary or final output line. If horizontal subsampling is included,
      // this goes from our buffer, otherwise it comes from the lowpass where the line
      // will end up, finally.
      assert(m_ppHBuffer[comp] == NULL);
      out = AllocLine(comp);
      m_ppHBuffer[comp] = out;
      //
      // Replicate at the edges.
      if (top == NULL)
        top = bottom;
      assert(top && bottom && center);
      //
      // Now filter.
      LONG *dst = out->m_pData;
      LONG *end = out->m_pData + m_pulPixelWidth[comp];
      LONG *tp  = top->m_pData;
      LONG *cp  = center->m_pData;
      LONG *bp  = bottom->m_pData;
      do {
        *dst++ = (*tp++ + (*cp++ << 1) + *bp++ + 1) >> 2;
      } while(dst < end);
      //
      // The new low pass is ready now in HBuffer. Whether it will be pushed
      // directly or filtered depends on whether horizontal filtering is done.
      //
      // The current top buffer is no longer required, leave it in the image,
      // though.
      m_ppTop[comp]    = NULL;
      // The center line is no longer required, too.
      m_ppCenter[comp] = NULL;
    } else {
      // An even line - just keep the buffer.
      assert(m_ppCenter[comp] == NULL);
      m_ppCenter[comp] = line;
      //
      // Special case if this is the last line of the image: This then generates the
      // last line of the low-pass, otherwise the line is just buffered.
      if (m_pulPixelHeight[comp] && m_pulY[comp] >= m_pulPixelHeight[comp] - 1) {
        struct Line *center  = m_ppCenter[comp];
        struct Line *bottom  = m_ppBottom[comp]; // Top line is replicated into bottom.
        struct Line *top     = m_ppBottom[comp];
        struct Line *out;
        //
        assert(m_ppHBuffer[comp] == NULL);
        out = AllocLine(comp);
        m_ppHBuffer[comp] = out;
        //
        //
        if (top && bottom) {
          // If there are lines now, filter. In the special case of only having a single
          // line, just replicate and set the high-pass to zero.
          LONG *dst = out->m_pData;
          LONG *end = out->m_pData + m_pulPixelWidth[comp];
          LONG *tp  = top->m_pData;
          LONG *cp  = center->m_pData;
          LONG *bp  = bottom->m_pData;
          do {
            *dst++ = (*tp++ + (*cp++ << 1) + *bp++ + 1) >> 2;
          } while(dst < end);
        } else {
          // Special case single line image: Just copy the data.
          memcpy(out->m_pData,center->m_pData,sizeof(LONG) * m_pulPixelWidth[comp]);
        }
      }
    }
  } else {
    // No vertical subsampling. Just take the line we got.
    m_ppHBuffer[comp] = line;
  }
  //
  // The vertically filtered (or not filtered) data is now in the HBuffer, or it is NULL
  // if no output has been generated this time.
  if (m_ppHBuffer[comp]) {
    // If here is horizontal filtering, apply now.
    if (m_bExpandH) {
      struct Line *out = m_pLowPass->AllocateLine(comp);
      LONG *dst        = out->m_pData;
      LONG *end        = out->m_pData + ((m_pulPixelWidth[comp] + 1) >> 1);
      LONG *src        = m_ppHBuffer[comp]->m_pData;
      // Replicate at the end.
      src[m_pulPixelWidth[comp]] = src[m_pulPixelWidth[comp] - 1];
      LONG left        = src[1]; // the left pixel is replicated from the right.
      LONG center,right;
      do {
        center = src[0];
        right  = src[1];
        *dst++ = (left + (center << 1) + right + 1) >> 2;
        left   = right;
        src   += 2;
      } while(dst < end);
      //
      // Done with it.
      m_pLowPass->PushLine(out,comp);
      if (m_bExpandV)
        FreeLine(m_ppHBuffer[comp],comp); // was a temporary.
      m_ppHBuffer[comp] = NULL;
    } else {
      struct Line *out = m_pLowPass->AllocateLine(comp);
      struct Line *src = m_ppHBuffer[comp];
      // Just push directly unfiltered. Must copy over as this class needs to
      // keep the original.
      memcpy(out->m_pData,src->m_pData,m_pulPixelWidth[comp] * sizeof(LONG));
      m_pLowPass->PushLine(out,comp); 
      if (m_bExpandV)
        FreeLine(m_ppHBuffer[comp],comp); // was a temporary.
      m_ppHBuffer[comp] = NULL;
    }
  }
  //
  // Next line.
  m_pulY[comp]++;
#else
  NOREF(line);
  NOREF(comp);
#endif
}
///

/// LineMerger::ResetToStartOfImage
// Rewind the image buffer to the start of the image
void LineMerger::ResetToStartOfImage(void)
{
#if ACCUSOFT_CODE
  UBYTE i;

  for(i = 0;i < m_ucCount;i++) {
    m_pppImage[i] = m_ppFirstLine + i;
    m_pulY[i]     = 0;
    if (m_ppVBuffer[i]) {
      FreeLine(m_ppVBuffer[i],i);
      m_ppVBuffer[i] = NULL;
    }
    if (m_ppHBuffer[i]) {
      FreeLine(m_ppHBuffer[i],i);
      m_ppHBuffer[i] = NULL;
    }
  }

  if (m_pHighPass) m_pHighPass->ResetToStartOfImage();
  if (m_pLowPass)  m_pLowPass->ResetToStartOfImage();
#endif
}
///

/// LineMerger::GenerateDifferentialImage
// Generate a differential image by pulling the reconstructed
// image from the low-pass and pushing the differential signal
// into the high-pass.
void LineMerger::GenerateDifferentialImage(void)
{
#if ACCUSOFT_CODE
  UBYTE comp;
  ULONG y;
  LONG shift = m_pHighPass->DCOffsetOf();  

  ResetToStartOfImage(); // also resets the subbands
  
  for(comp = 0;comp < m_ucCount;comp++) {
    ULONG height = m_pulPixelHeight[comp];

    // If this is NULL, then the data has already been pushed
    // before, probably because we measured in the iteration before.
    if (m_ppFirstLine[comp]) {
      for(y = 0;y < height;y++) {
        struct Line *low  = GetNextExpandedLowPassLine(comp);
        struct Line *high = m_pHighPass->AllocateLine(comp);
        struct Line *top  = m_ppFirstLine[comp]; // Old buffered lines.
        //
        if (top == NULL)
          JPG_THROW(OBJECT_DOESNT_EXIST,"LineMerger::GenerateDifferentialImage",
                    "cannot create the next frame of the differential image, the previous frame is still incomplete");
        //
        // Now compute the difference. This becomes the high-pass output.
        LONG *dst = high->m_pData;
        LONG *end = high->m_pData + m_pulPixelWidth[comp];
        LONG *rec = low->m_pData;
        LONG *org = top->m_pData;
        if (m_pHighPass->isLossless()) {
          do {
            // The DCT always removes the level shift, so simply add it here.
            *dst = ((*org++ >> COLOR_BITS) - ((*rec++ + shift) >> COLOR_BITS)) << COLOR_BITS;
          } while(++dst < end);
        } else {
          do {
            // The DCT always removes the level shift, so simply add it here.
            *dst = *org++ - *rec++ + shift;
          } while(++dst < end);
        }
        //
        // Done with it. Also releases high.
        m_pHighPass->PushLine(high,comp);
        //
        // And the corresponding source line can also go.
        m_ppFirstLine[comp]     = top->m_pNext;
        FreeLine(top,comp);
      }
    }
  }
#endif
}
///

/// LineMerger::PostImageHeight
// Post the height of the frame in lines. This happens
// when the DNL marker is processed.
void LineMerger::PostImageHeight(ULONG lines)
{
  LineAdapter::PostImageHeight(lines);

#if ACCUSOFT_CODE
  assert(m_pulPixelHeight);
  
  if (m_pLowPass)
    m_pLowPass->PostImageHeight((lines + 1) >> 1);
  if (m_pHighPass)
    m_pHighPass->PostImageHeight(lines);

  for(UBYTE i = 0;i < m_ucCount;i++) {
    class Component *comp = m_pFrame->ComponentOf(i);
    UBYTE suby            = comp->SubYOf();
    m_pulPixelHeight[i]   = (lines + suby - 1) / suby;
  }
#endif
}
///