#pragma warning disable 162 // unreachable code private void DecodeType2Image (RLEDecodeContext context) { if (m_info.BPP != 8) throw new InvalidFormatException(); throw new NotImplementedException ("Arithmetic compression not implemented"); if (EriCode.ArithmeticCode == m_info.Architecture) { // (context as ArithmeticContext).InitArithmeticContext (8); m_ptrLineBuf = new sbyte[m_info.Width*4]; } else throw new NotImplementedException(); int dst = m_dst; for (int nPosY = 0; nPosY < (int)m_info.Height; ++nPosY) { if (context.DecodeBytes (m_ptrLineBuf, m_info.Width) < m_info.Width) throw new InvalidFormatException(); for (int x = 0; x < (int)m_info.Width; ++x) m_output[dst+x] = (byte)m_ptrLineBuf[4 * x]; dst += m_dwBytesPerLine; } }
private void DecodeLosslessImage (RLEDecodeContext context) { context.FlushBuffer(); uint nERIVersion = context.GetNBits (8); uint fOpTable = context.GetNBits (8); uint fEncodeType = context.GetNBits (8); uint nBitCount = context.GetNBits (8); if (0 != fOpTable || 0 != (fEncodeType & 0xFE)) { throw new InvalidFormatException(); } switch (nERIVersion) { case 1: if (nBitCount != 0) throw new InvalidFormatException(); break; case 2: if (nBitCount != 0 || fEncodeType != 0) throw new InvalidFormatException(); DecodeType2Image (context); return; case 8: if (nBitCount != 8) throw new InvalidFormatException(); break; case 16: if ((nBitCount != 8) || (fEncodeType != 0)) throw new InvalidFormatException(); break; default: throw new InvalidFormatException(); } m_nDstPixelBytes = m_info.BPP >> 3; m_nDstLineBytes = m_dwBytesPerLine; var pfnRestoreFunc = GetLLRestoreFunc (m_info.FormatType, m_info.BPP); if (null == pfnRestoreFunc) throw new InvalidFormatException(); if (EriCode.Nemesis == m_info.Architecture) { Debug.Assert (m_pProbERISA != null); m_pProbERISA.Initialize(); } int i; int ptrNextOperation = 0; // index within m_ptrOperations if ((0 != (fEncodeType & 1)) && (m_nChannelCount >= 3)) { if (m_info.Architecture == EriCode.Nemesis) throw new InvalidFormatException(); int nAllBlockCount = m_nWidthBlocks * m_nHeightBlocks; for (i = 0; i < nAllBlockCount; i++) { if (EriCode.RunlengthGamma == m_info.Architecture) { m_ptrOperations[i] = (byte)(context.GetNBits(4) | 0xC0); } else { Debug.Assert (EriCode.RunlengthHuffman == m_info.Architecture); m_ptrOperations[i] = (byte)(context as HuffmanDecodeContext).GetHuffmanCode (m_pHuffmanTree); } } } if (context.GetABit() != 0) throw new InvalidFormatException(); if (EriCode.RunlengthGamma == m_info.Architecture) { if (0 != (fEncodeType & 1)) { context.InitGammaContext(); } } else if (EriCode.RunlengthHuffman == m_info.Architecture) { (context as HuffmanDecodeContext).PrepareToDecodeERINACode(); } else { Debug.Assert (EriCode.Nemesis == m_info.Architecture); (context as ProbDecodeContext).PrepareToDecodeERISACode(); } int nWidthSamples = m_nChannelCount * m_nWidthBlocks * m_nBlockSize; for (i = 0; i < nWidthSamples; ++i) m_ptrLineBuf[i] = 0; int nAllBlockLines = m_nBlockSize * m_nChannelCount; int nLeftHeight = (int)m_info.Height; for (int nPosY = 0; nPosY < m_nHeightBlocks; ++nPosY) { int nColumnBufSamples = m_nBlockSize * m_nChannelCount; for (i = 0; i < nColumnBufSamples; ++i) m_ptrColumnBuf[i] = 0; m_ptrDstBlock = m_dst + nPosY * m_dwBytesPerLine * m_nBlockSize; m_nDstHeight = Math.Min (m_nBlockSize, nLeftHeight); int nLeftWidth = (int)m_info.Width; int ptrNextLineBuf = 0; // m_ptrLineBuf; for (int nPosX = 0; nPosX < m_nWidthBlocks; ++nPosX) { m_nDstWidth = Math.Min (m_nBlockSize, nLeftWidth); uint dwOperationCode; if (m_nChannelCount >= 3) { if (0 != (fEncodeType & 1)) { dwOperationCode = m_ptrOperations[ptrNextOperation++]; } else if (m_info.Architecture == EriCode.RunlengthHuffman) { dwOperationCode = (uint)(context as HuffmanDecodeContext).GetHuffmanCode (m_pHuffmanTree); } else if (m_info.Architecture == EriCode.Nemesis) { dwOperationCode = (uint)(context as ProbDecodeContext).DecodeERISACode (m_pProbERISA); } else { Debug.Assert (EriCode.RunlengthGamma == m_info.Architecture); dwOperationCode = context.GetNBits (4) | 0xC0; context.InitGammaContext(); } } else { if (EriType.Gray == m_info.FormatType) { dwOperationCode = 0xC0; } else { dwOperationCode = 0; } if (0 == (fEncodeType & 1) && m_info.Architecture == EriCode.RunlengthGamma) { context.InitGammaContext(); } } if (context.DecodeBytes (m_ptrArrangeBuf, (uint)m_nBlockSamples) < m_nBlockSamples) { throw new InvalidFormatException(); } PerformOperation (dwOperationCode, nAllBlockLines, m_ptrLineBuf, ptrNextLineBuf); ptrNextLineBuf += nColumnBufSamples; pfnRestoreFunc(); m_ptrDstBlock += m_nDstPixelBytes * m_nBlockSize; nLeftWidth -= m_nBlockSize; } nLeftHeight -= m_nBlockSize; } }