Ejemplo n.º 1
0
        private static void Decode2DScanline(ref BitReader bitReader, bool whiteIsZero, ReferenceScanline referenceScanline, Span <byte> scanline)
        {
            int width = scanline.Length;
            CcittDecodingTable currentTable = CcittDecodingTable.WhiteInstance;
            CcittDecodingTable otherTable   = CcittDecodingTable.BlackInstance;
            CcittTwoDimensionalDecodingTable decodingTable = CcittTwoDimensionalDecodingTable.Instance;
            CcittTwoDimensionalCodeValue     tableEntry;
            const int PeekCount = CcittTwoDimensionalDecodingTable.PeekCount;

            // 2D Encoding variables.
            int  a0 = -1, a1, b1;
            byte fillByte = whiteIsZero ? (byte)0 : (byte)255;

            // Process every code word in this scanline
            int unpacked = 0;

            while (true)
            {
                // Read next code word and advance pass it.
                int value = (int)bitReader.Peek(PeekCount); // PeekCount = 12

                // Special case handling for EOL.
                // Lucky we don't need to peek again for EOL, because PeekCount(12) is excatly the length of EOL code.
                if (value == 0b000000000001)
                {
                    scanline.Fill(fillByte);
                    break;
                }

                // Look up in the table and advance past this code.
                if (!decodingTable.TryLookup(value, out tableEntry))
                {
                    throw new InvalidDataException();
                }
                bitReader.Advance(tableEntry.BitsRequired);

                // Update 2D Encoding variables.
                b1 = referenceScanline.FindB1(a0, fillByte);

                // Switch on the code word.
                switch (tableEntry.Type)
                {
                case CcittTwoDimensionalCodeType.Pass:
                    int b2 = referenceScanline.FindB2(b1);
                    scanline.Slice(unpacked, b2 - unpacked).Fill(fillByte);
                    unpacked = b2;
                    a0       = b2;
                    break;

                case CcittTwoDimensionalCodeType.Horizontal:
                    // Decode M(a0a1)
                    int runLength = currentTable.DecodeRun(ref bitReader);
                    if ((uint)runLength > (uint)(scanline.Length - unpacked))
                    {
                        throw new InvalidOperationException();
                    }
                    scanline.Slice(unpacked, runLength).Fill(fillByte);
                    unpacked += runLength;
                    fillByte  = (byte)~fillByte;
                    CcittHelper.SwapTable(ref currentTable, ref otherTable);
                    // Decode M(a1a2)
                    runLength = currentTable.DecodeRun(ref bitReader);
                    if ((uint)runLength > (uint)(scanline.Length - unpacked))
                    {
                        throw new InvalidOperationException();
                    }
                    scanline.Slice(unpacked, runLength).Fill(fillByte);
                    unpacked += runLength;
                    fillByte  = (byte)~fillByte;
                    CcittHelper.SwapTable(ref currentTable, ref otherTable);
                    // Prepare next a0
                    a0 = unpacked;
                    break;

                case CcittTwoDimensionalCodeType.Vertical0:
                    a1 = b1;
                    scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte);
                    unpacked = a1;
                    a0       = a1;
                    fillByte = (byte)~fillByte;
                    CcittHelper.SwapTable(ref currentTable, ref otherTable);
                    break;

                case CcittTwoDimensionalCodeType.VerticalR1:
                    a1 = b1 + 1;
                    scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte);
                    unpacked = a1;
                    a0       = a1;
                    fillByte = (byte)~fillByte;
                    CcittHelper.SwapTable(ref currentTable, ref otherTable);
                    break;

                case CcittTwoDimensionalCodeType.VerticalR2:
                    a1 = b1 + 2;
                    scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte);
                    unpacked = a1;
                    a0       = a1;
                    fillByte = (byte)~fillByte;
                    CcittHelper.SwapTable(ref currentTable, ref otherTable);
                    break;

                case CcittTwoDimensionalCodeType.VerticalR3:
                    a1 = b1 + 3;
                    scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte);
                    unpacked = a1;
                    a0       = a1;
                    fillByte = (byte)~fillByte;
                    CcittHelper.SwapTable(ref currentTable, ref otherTable);
                    break;

                case CcittTwoDimensionalCodeType.VerticalL1:
                    a1 = b1 - 1;
                    scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte);
                    unpacked = a1;
                    a0       = a1;
                    fillByte = (byte)~fillByte;
                    CcittHelper.SwapTable(ref currentTable, ref otherTable);
                    break;

                case CcittTwoDimensionalCodeType.VerticalL2:
                    a1 = b1 - 2;
                    scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte);
                    unpacked = a1;
                    a0       = a1;
                    fillByte = (byte)~fillByte;
                    CcittHelper.SwapTable(ref currentTable, ref otherTable);
                    break;

                case CcittTwoDimensionalCodeType.VerticalL3:
                    a1 = b1 - 3;
                    scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte);
                    unpacked = a1;
                    a0       = a1;
                    fillByte = (byte)~fillByte;
                    CcittHelper.SwapTable(ref currentTable, ref otherTable);
                    break;

                default:
                    throw new NotSupportedException("1D and 2D Extensions not supportted.");
                }

                // This line is fully unpacked. Should exit and process next line.
                if (unpacked == width)
                {
                    break;
                }
                else if (unpacked > width)
                {
                    throw new InvalidDataException();
                }
            }
        }
Ejemplo n.º 2
0
        private static void Encode2DScanline(ref BitWriter2 bitWriter, ReferenceScanline referenceScanline, ReadOnlySpan <byte> scanline)
        {
            int width = scanline.Length;
            CcittEncodingTable currentTable   = CcittEncodingTable.WhiteInstance;
            CcittEncodingTable otherTable     = CcittEncodingTable.BlackInstance;
            CodingScanline     codingScanline = new CodingScanline(whiteIsZero: true, scanline);

            byte a0Byte = 0;
            int  a0     = -1;

            while (true)
            {
                int a1 = codingScanline.FindA1(a0, a0Byte);
                int b1 = referenceScanline.FindB1(a0, a0Byte);
                int b2 = referenceScanline.FindB2(b1);

                if (b2 < a1)
                {
                    EncodePassCode(ref bitWriter);
                    a0 = b2;
                }
                else
                {
                    int a1b1 = b1 - a1;
                    if (a1b1 >= -3 && a1b1 <= 3)
                    {
                        // Vertical mode coding
                        switch (a1b1)
                        {
                        case 3:
                            EncodeVerticalL3Code(ref bitWriter);
                            break;

                        case 2:
                            EncodeVerticalL2Code(ref bitWriter);
                            break;

                        case 1:
                            EncodeVerticalL1Code(ref bitWriter);
                            break;

                        case 0:
                            EncodeVertical0Code(ref bitWriter);
                            break;

                        case -1:
                            EncodeVerticalR1Code(ref bitWriter);
                            break;

                        case -2:
                            EncodeVerticalR2Code(ref bitWriter);
                            break;

                        case -3:
                            EncodeVerticalR3Code(ref bitWriter);
                            break;

                        default:
                            break;
                        }

                        CcittHelper.SwapTable(ref currentTable, ref otherTable);
                        a0 = a1;
                    }
                    else
                    {
                        int a2 = codingScanline.FindA2(a1);
                        EncodeHorizontalCode(ref bitWriter);

                        currentTable.EncodeRun(ref bitWriter, a1 - a0);
                        otherTable.EncodeRun(ref bitWriter, a2 - a1);

                        a0 = a2;
                    }
                }

                if (a0 >= width)
                {
                    break;
                }
                a0Byte = scanline[a0];
            }
        }