CSJ2K.j2k.entropy.encoder.StdEntropyCoder.checkEndOfPassFF C# (CSharp) Method

checkEndOfPassFF() static private method

Ensures that at the end of a non-terminated coding pass there is not a 0xFF byte, modifying the stored rates if necessary.

Due to error resiliance reasons, a coding pass should never have its last byte be a 0xFF, since that can lead to the emulation of a resync marker. This method checks if that is the case, and reduces the rate for a given pass if necessary. The ommitted 0xFF will be synthetized by the decoder if necessary, as required by JPEG 2000. This method should only be called once that the entire code-block is coded.

Passes that are terminated are not checked for the 0xFF byte, since it is assumed that the termination procedure does not output any trailing 0xFF. Checking the terminated segments would involve much more than just modifying the stored rates.

NOTE: It is assumed by this method that the coded data does not contain consecutive 0xFF bytes, as is the case with the MQ and 'arithemetic coding bypass' bit stuffing policy. However, the termination policies used should also respect this requirement.

static private checkEndOfPassFF ( byte data, int rates, bool isterm, int n ) : void
data byte The coded data for the code-block /// ///
rates int The rate (i.e. accumulated number of bytes) for each /// coding pass /// ///
isterm bool An array of flags indicating, for each pass, if it is /// terminated or not. If null it is assumed that no pass is terminated, /// except the last one. /// ///
n int The number of coding passes /// ///
return void
		static private void  checkEndOfPassFF(byte[] data, int[] rates, bool[] isterm, int n)
		{
			int dp; // the position to test in 'data'
			
			// If a pass ends in 0xFF we need to reduce the number of bytes in it,
			// so that it does not end in 0xFF. We only need to go back one byte
			// since there can be no consecutive 0xFF bytes.
			
			// If there are no terminated passes avoid the test on 'isterm'
			if (isterm == null)
			{
				for (n--; n >= 0; n--)
				{
					dp = rates[n] - 1;
					if (dp >= 0 && (data[dp] == (byte)0xFF))
					{
						rates[n]--;
					}
				}
			}
			else
			{
				for (n--; n >= 0; n--)
				{
					if (!isterm[n])
					{
						dp = rates[n] - 1;
						if (dp >= 0 && (data[dp] == (byte)0xFF))
						{
							rates[n]--;
						}
					}
				}
			}
		}