static private int rawMagRefPass(CBlkWTData srcblk, BitToByteOutput bout, bool doterm, int bp, int[] state, int[] fm, int[] ratebuf, int pidx, int ltpidx, int options)
{
int j, sj; // The state index for line and stripe
int k, sk; // The data index for line and stripe
int dscanw; // The data scan-width
int sscanw; // The state scan-width
int jstep; // Stripe to stripe step for 'sj'
int kstep; // Stripe to stripe step for 'sk'
int stopsk; // The loop limit on the variable sk
int csj; // Local copy (i.e. cached) of 'state[j]'
int mask; // The mask for the current bit-plane
int[] data; // The data buffer
int dist; // The distortion reduction for this pass
int shift; // Shift amount for distortion
int upshift; // Shift left amount for distortion
int downshift; // Shift right amount for distortion
int normval; // The normalized sample magnitude value
int s; // The stripe index
int nstripes; // The number of stripes in the code-block
int sheight; // Height of the current stripe
int nsym = 0;
// Initialize local variables
dscanw = srcblk.scanw;
sscanw = srcblk.w + 2;
jstep = sscanw * CSJ2K.j2k.entropy.StdEntropyCoderOptions.STRIPE_HEIGHT / 2 - srcblk.w;
kstep = dscanw * CSJ2K.j2k.entropy.StdEntropyCoderOptions.STRIPE_HEIGHT - srcblk.w;
mask = 1 << bp;
data = (int[]) srcblk.Data;
nstripes = (srcblk.h + CSJ2K.j2k.entropy.StdEntropyCoderOptions.STRIPE_HEIGHT - 1) / CSJ2K.j2k.entropy.StdEntropyCoderOptions.STRIPE_HEIGHT;
dist = 0;
// We use the bit just coded plus MSE_LKP_BITS-1 bits below the bit
// just coded for distortion estimation.
shift = bp - (MSE_LKP_BITS - 1);
upshift = (shift >= 0)?0:- shift;
downshift = (shift <= 0)?0:shift;
// Code stripe by stripe
sk = srcblk.offset;
sj = sscanw + 1;
for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep)
{
sheight = (s != 0)?CSJ2K.j2k.entropy.StdEntropyCoderOptions.STRIPE_HEIGHT:srcblk.h - (nstripes - 1) * CSJ2K.j2k.entropy.StdEntropyCoderOptions.STRIPE_HEIGHT;
stopsk = sk + srcblk.w;
// Scan by set of 1 stripe column at a time
for (; sk < stopsk; sk++, sj++)
{
// Do half top of column
j = sj;
csj = state[j];
// If any of the two samples is significant and not yet
// visited in the current bit-plane we can not skip them
if ((((SupportClass.URShift(csj, 1)) & (~ csj)) & VSTD_MASK_R1R2) != 0)
{
k = sk;
// Scan first row
if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1)
{
// Code bit "raw"
bout.writeBit(SupportClass.URShift((data[k] & mask), bp));
nsym++;
// No need to set STATE_PREV_MR_R1 since all magnitude
// refinement passes to follow are "raw"
// Update distortion
normval = (data[k] >> downshift) << upshift;
dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
}
if (sheight < 2)
continue;
// Scan second row
if ((csj & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2)
{
k += dscanw;
// Code bit "raw"
bout.writeBit(SupportClass.URShift((data[k] & mask), bp));
nsym++;
// No need to set STATE_PREV_MR_R2 since all magnitude
// refinement passes to follow are "raw"
// Update distortion
normval = (data[k] >> downshift) << upshift;
dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
}
}
// Do half bottom of column
if (sheight < 3)
continue;
j += sscanw;
csj = state[j];
// If any of the two samples is significant and not yet
// visited in the current bit-plane we can not skip them
if ((((SupportClass.URShift(csj, 1)) & (~ csj)) & VSTD_MASK_R1R2) != 0)
{
k = sk + (dscanw << 1);
// Scan first row
if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1)
{
// Code bit "raw"
bout.writeBit(SupportClass.URShift((data[k] & mask), bp));
nsym++;
// No need to set STATE_PREV_MR_R1 since all magnitude
// refinement passes to follow are "raw"
// Update distortion
normval = (data[k] >> downshift) << upshift;
dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
}
if (sheight < 4)
continue;
// Scan second row
if ((state[j] & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2)
{
k += dscanw;
// Code bit "raw"
bout.writeBit(SupportClass.URShift((data[k] & mask), bp));
nsym++;
// No need to set STATE_PREV_MR_R2 since all magnitude
// refinement passes to follow are "raw"
// Update distortion
normval = (data[k] >> downshift) << upshift;
dist += fm[normval & ((1 << MSE_LKP_BITS) - 1)];
}
}
}
}
// Get length and terminate if needed
if (doterm)
{
ratebuf[pidx] = bout.terminate();
}
else
{
ratebuf[pidx] = bout.length();
}
// Add length of previous segments, if any
if (ltpidx >= 0)
{
ratebuf[pidx] += ratebuf[ltpidx];
}
// Return the reduction in distortion
return dist;
}