Example #1
0
        public Picture decodeField(MemoryStream data, int[][] data2, int field, int step)
        {
            Picture result = null;

            FrameHeader header = null;

            VLC[] huffTables = new VLC[] { JpegConst.YDC_DEFAULT, JpegConst.CDC_DEFAULT, JpegConst.YAC_DEFAULT,
                                           JpegConst.CAC_DEFAULT };
            int[][]    quant = new int[4][];
            ScanHeader scan  = null;

            while (data.hasRemaining())
            {
                int marker = data.get() & 0xff;
                if (marker == 0)
                {
                    continue;
                }
                if (marker != 0xFF)
                {
                    throw new Exception("@" + data.Position + " Marker expected: 0x"
                                        + marker.ToString("X"));
                }

                int b;
                while ((b = data.get() & 0xff) == 0xff)
                {
                    ;
                }
                // Debug.trace("%s", JpegConst.toString(b));
                if (b == JpegConst.SOF0)
                {
                    header = FrameHeader.read(data);
                    // Debug.trace("    %s", image.frame);
                }
                else if (b == JpegConst.DHT)
                {
                    int          len1 = data.getShort() & 0xffff;
                    MemoryStream buf  = StreamExtensions.read(data, len1 - 2);
                    while (buf.hasRemaining())
                    {
                        int tableNo = buf.get() & 0xff;
                        huffTables[(tableNo & 1) | ((tableNo >> 3) & 2)] = readHuffmanTable(buf);
                    }
                }
                else if (b == JpegConst.DQT)
                {
                    int          len4 = data.getShort() & 0xffff;
                    MemoryStream buf  = StreamExtensions.read(data, len4 - 2);
                    while (buf.hasRemaining())
                    {
                        int ind = buf.get() & 0xff;
                        quant[ind] = readQuantTable(buf);
                    }
                }
                else if (b == JpegConst.SOS)
                {
                    if (scan != null)
                    {
                        throw new Exception("unhandled - more than one scan header");
                    }
                    scan = ScanHeader.read(data);
                    // Debug.trace("    %s", image.scan);
                    result = decodeScan(readToMarker(data), header, scan, huffTables, quant, data2, field, step);
                }
                else if (b == JpegConst.SOI || (b >= JpegConst.RST0 && b <= JpegConst.RST7))
                {
                    // Nothing
                }
                else if (b == JpegConst.EOI)
                {
                    break;
                }
                else if (b >= JpegConst.APP0 && b <= JpegConst.COM)
                {
                    int len3 = data.getShort() & 0xffff;
                    StreamExtensions.read(data, len3 - 2);
                }
                else if (b == JpegConst.DRI)
                {
                    int lr = data.getShort() & 0xffff;

                    int ri = data.getShort() & 0xffff;
                    // Debug.trace("DRI Lr: %d Ri: %d", lr, ri);

                    //Asserts.assertEquals(0, ri);
                }
                else
                {
                    throw new Exception("unhandled marker " + JpegConst.toString(b));
                }
            }

            return(result);
        }