Inheritance: System.IO.BinaryReader
        public JpegDecoder(Stream input)
        {
            jpegReader = new JPEGBinaryReader(input);

            if (jpegReader.GetNextMarker() != JPEGMarker.SOI)
                throw new Exception("Failed to find SOI marker.");
        }
Example #2
0
 public JpegDecoder(Stream input)
 {
     this.jpegReader = new JPEGBinaryReader(input);
     if (this.jpegReader.GetNextMarker() != 0xd8)
     {
         throw new Exception("Failed to find SOI marker.");
     }
 }
Example #3
0
 public float decode_dc_coefficient(JPEGBinaryReader JPEGStream)
 {
     int n = this.DCTable.Decode(JPEGStream);
     float num2 = JPEGStream.ReadBits(n);
     num2 = HuffmanTable.Extend((int) num2, n);
     num2 = this.previousDC + num2;
     this.previousDC = num2;
     return num2;
 }
        public void DecodeScanBaseline(byte numberOfComponents, byte[] componentSelector, int resetInterval, JPEGBinaryReader jpegReader, ref byte marker)
        {
            // Set the decode function for all the components
            for (int compIndex = 0; compIndex < numberOfComponents; compIndex++)
            {
                JpegComponent comp = Scan.GetComponentById(componentSelector[compIndex]);
                comp.Decode = comp.DecodeBaseline;
            }

            DecodeScan(numberOfComponents, componentSelector, resetInterval, jpegReader, ref marker);
        }
Example #5
0
 public int Decode(JPEGBinaryReader JPEGStream)
 {
     int index = 0;
     short num2 = (short) JPEGStream.ReadBits(1);
     while (num2 > this.maxcode[index])
     {
         index++;
         num2 = (short) (num2 << 1);
         num2 = (short) (num2 | ((short) JPEGStream.ReadBits(1)));
     }
     int num3 = this.huffval[num2 + this.valptr[index]];
     if (num3 < 0)
     {
         num3 = 0x100 + num3;
     }
     return num3;
 }
Example #6
0
 internal void decode_ac_coefficients(JPEGBinaryReader JPEGStream, float[] zz)
 {
     for (int i = 1; i < 0x40; i++)
     {
         int t = this.ACTable.Decode(JPEGStream);
         int num3 = t >> 4;
         t &= 15;
         if (t != 0)
         {
             i += num3;
             zz[i] = HuffmanTable.Extend(JPEGStream.ReadBits(t), t);
         }
         else
         {
             if (num3 != 15)
             {
                 break;
             }
             i += 15;
         }
     }
 }
Example #7
0
        public void DecodeDCFirst(JPEGBinaryReader stream, float[] dest)
        {
            float[] datablock = new float[64];
            int s = DCTable.Decode(stream);
            int r = stream.ReadBits(s);
            s = HuffmanTable.Extend(r, s);
            s = (int)previousDC + s;
            previousDC = s;

            dest[0] = s << successiveLow;
        }
Example #8
0
 public void DecodeBaseline(JPEGBinaryReader stream, float[] dest)
 {
     float dc = decode_dc_coefficient(stream);
     decode_ac_coefficients(stream, dest);
     dest[0] = dc;
 }
 /// <summary>Figure F.16 - Reads the huffman code bit-by-bit.</summary>
 public int Decode(JPEGBinaryReader JPEGStream)
 {
     int i = 0;
     short code = (short)JPEGStream.ReadBits(1);
     while (code > maxcode[i])
     {
         i++;
         code <<= 1;
         code |= (short)JPEGStream.ReadBits(1);
     }
     int val = huffval[code + (valptr[i])];
     if (val < 0)
         val = 256 + val;
     return val;
 }
Example #10
0
 public void DecodeBaseline(JPEGBinaryReader stream, float[] dest)
 {
     float num = this.decode_dc_coefficient(stream);
     this.decode_ac_coefficients(stream, dest);
     dest[0] = num;
 }
Example #11
0
 /// <summary>
 /// Generated from text on F-22, F.2.2.1 - Huffman decoding of DC
 /// coefficients on ISO DIS 10918-1. Requirements and Guidelines.
 /// </summary>
 /// <param name="JPEGStream">Stream that contains huffman bits</param>
 /// <returns>DC coefficient</returns>
 public float decode_dc_coefficient(JPEGBinaryReader JPEGStream)
 {
     int t = DCTable.Decode(JPEGStream);
     float diff = JPEGStream.ReadBits(t);
     diff = HuffmanTable.Extend((int)diff, t);
     diff = (previousDC + diff);
     previousDC = diff;
     return diff;
 }
Example #12
0
        public void DecodeACRefine(JPEGBinaryReader stream, float[] dest)
        {
            int p1 = 1 << successiveLow;
            int m1 = (-1) << successiveLow;

            int k = spectralStart;

            if (stream.eob_run == 0)
                for (; k <= spectralEnd; k++)
                {
                    #region Decode and check S

                    int s = ACTable.Decode(stream);
                    int r = s >> 4;
                    s &= 15;

                    if (s != 0)
                    {
                        if (s != 1)
                            throw new Exception("Decode Error");

                        if (stream.ReadBits(1) == 1)
                            s = p1;
                        else
                            s = m1;
                    }
                    else
                    {
                        if (r != 15)
                        {
                            stream.eob_run = 1 << r;

                            if (r > 0)
                                stream.eob_run += stream.ReadBits(r);
                            break;
                        }

                    } // if (s != 0)

                    #endregion

                    // Apply the update
                    do
                    {
                        if (dest[k] != 0)
                        {
                            if (stream.ReadBits(1) == 1)
                            {
                                if (((int)dest[k] & p1) == 0)
                                {
                                    if (dest[k] >= 0)
                                        dest[k] += p1;
                                    else
                                        dest[k] += m1;
                                }
                            }
                        }
                        else
                        {
                            if (--r < 0)
                                break;
                        }

                        k++;

                    } while (k <= spectralEnd);

                    if( (s != 0) && k < 64)
                    {
                        dest[k] = s;
                    }
                } // for k = start ... end


            if (stream.eob_run > 0)
            {
                for (; k <= spectralEnd; k++)
                {
                    if (dest[k] != 0)
                    {
                        if (stream.ReadBits(1) == 1)
                        {
                            if (((int)dest[k] & p1) == 0)
                            {
                                if (dest[k] >= 0)
                                    dest[k] += p1;
                                else
                                    dest[k] += m1;
                            }
                        }
                    }
                }

                stream.eob_run--;
            }
        }
Example #13
0
        //private void DecodeScan(byte numberOfComponents, byte[] componentSelector, int resetInterval, JPEGBinaryReader jpegReader, ref byte marker)
        //{
        //    jpegReader.eob_run = 0;
        //    int idx = 0;
        //    int num2 = 0;
        //    int i = 0;
        //    int j = 0;
        //    int num5 = 0;
        //    long position = jpegReader.BaseStream.Position;
        //    while (true)
        //    {
        //        JpegComponent componentById;
        //        int num9;
        //        if ((this.ProgressUpdateMethod != null) && (jpegReader.BaseStream.Position >= (position + JpegDecoder.ProgressUpdateByteInterval)))
        //        {
        //            position = jpegReader.BaseStream.Position;
        //            this.ProgressUpdateMethod(position);
        //        }
        //        try
        //        {
        //            if (numberOfComponents == 1)
        //            {
        //                componentById = this.Scan.GetComponentById(componentSelector[0]);
        //                componentById.SetBlock(idx);
        //                componentById.DecodeMCU(jpegReader, i, j);
        //                int num7 = this.mcus_per_row(componentById);
        //                int num8 = (int) Math.Ceiling(((double) this.Width) / ((double) (8 * componentById.factorH)));
        //                i++;
        //                num5++;
        //                if (i == componentById.factorH)
        //                {
        //                    i = 0;
        //                    idx++;
        //                }
        //                if ((num5 % num7) == 0)
        //                {
        //                    num5 = 0;
        //                    j++;
        //                    if (j == componentById.factorV)
        //                    {
        //                        if (i != 0)
        //                        {
        //                            idx++;
        //                            i = 0;
        //                        }
        //                        j = 0;
        //                    }
        //                    else
        //                    {
        //                        idx -= num8;
        //                        if (i != 0)
        //                        {
        //                            idx++;
        //                            i = 0;
        //                        }
        //                    }
        //                }
        //            }
        //            else
        //            {
        //                num9 = 0;
        //                while (num9 < numberOfComponents)
        //                {
        //                    componentById = this.Scan.GetComponentById(componentSelector[num9]);
        //                    componentById.SetBlock(num2);
        //                    for (int k = 0; k < componentById.factorV; k++)
        //                    {
        //                        for (int m = 0; m < componentById.factorH; m++)
        //                        {
        //                            componentById.DecodeMCU(jpegReader, m, k);
        //                        }
        //                    }
        //                    num9++;
        //                }
        //                idx++;
        //                num2++;
        //            }
        //        }
        //        catch (JPEGMarkerFoundException exception)
        //        {
        //            marker = exception.Marker;
        //            if (((((marker != 0xd0) && (marker != 0xd1)) && ((marker != 210) && (marker != 0xd3))) && (((marker != 0xd4) && (marker != 0xd5)) && (marker != 0xd6))) && (marker != 0xd7))
        //            {
        //                return;
        //            }
        //            for (num9 = 0; num9 < numberOfComponents; num9++)
        //            {
        //                componentById = this.Scan.GetComponentById(componentSelector[num9]);
        //                if (num9 > 1)
        //                {
        //                    componentById.padMCU(num2, resetInterval - idx);
        //                }
        //                componentById.resetInterval();
        //            }
        //            num2 += resetInterval - idx;
        //            idx = 0;
        //        }
        //    }
        //}

        //public void DecodeScanBaseline(byte numberOfComponents, byte[] componentSelector, int resetInterval, JPEGBinaryReader jpegReader, ref byte marker)
        //{
        //    for (int i = 0; i < numberOfComponents; i++)
        //    {
        //        JpegComponent componentById = this.Scan.GetComponentById(componentSelector[i]);
        //        componentById.Decode = new JpegComponent.DecodeFunction(componentById.DecodeBaseline);
        //    }
        //    this.DecodeScan(numberOfComponents, componentSelector, resetInterval, jpegReader, ref marker);
        //}

        public void DecodeScanProgressive(byte successiveApproximation, byte startSpectralSelection, byte endSpectralSelection, byte numberOfComponents, byte[] componentSelector, int resetInterval, JPEGBinaryReader jpegReader, ref byte marker)
        {
            byte num = (byte) (successiveApproximation >> 4);
            byte num2 = (byte) (successiveApproximation & 15);
            if ((startSpectralSelection > endSpectralSelection) || (endSpectralSelection > 0x3f))
            {
                throw new Exception("Bad spectral selection.");
            }
            bool flag = startSpectralSelection == 0;
            bool flag2 = num != 0;
            if (flag)
            {
                if (endSpectralSelection != 0)
                {
                    throw new Exception("Bad spectral selection for DC only scan.");
                }
            }
            else if (numberOfComponents > 1)
            {
                throw new Exception("Too many components for AC scan!");
            }
            for (int i = 0; i < numberOfComponents; i++)
            {
                JpegComponent componentById = this.Scan.GetComponentById(componentSelector[i]);
                componentById.successiveLow = num2;
                if (flag)
                {
                    if (flag2)
                    {
                        componentById.Decode = new JpegComponent.DecodeFunction(componentById.DecodeDCRefine);
                    }
                    else
                    {
                        componentById.Decode = new JpegComponent.DecodeFunction(componentById.DecodeDCFirst);
                    }
                }
                else
                {
                    componentById.spectralStart = startSpectralSelection;
                    componentById.spectralEnd = endSpectralSelection;
                    if (flag2)
                    {
                        componentById.Decode = new JpegComponent.DecodeFunction(componentById.DecodeACRefine);
                    }
                    else
                    {
                        componentById.Decode = new JpegComponent.DecodeFunction(componentById.DecodeACFirst);
                    }
                }
            }
         //   this.DecodeScan(numberOfComponents, componentSelector, resetInterval, jpegReader, ref marker);
        }
Example #14
0
 public void DecodeACFirst(JPEGBinaryReader stream, float[] zz)
 {
     if (stream.eob_run > 0)
     {
         stream.eob_run--;
     }
     else
     {
         for (int i = this.spectralStart; i <= this.spectralEnd; i++)
         {
             int t = this.ACTable.Decode(stream);
             int n = t >> 4;
             t &= 15;
             if (t != 0)
             {
                 i += n;
                 t = HuffmanTable.Extend(stream.ReadBits(t), t);
                 zz[i] = t << this.successiveLow;
             }
             else
             {
                 if (n != 15)
                 {
                     stream.eob_run = ((int) 1) << n;
                     if (n != 0)
                     {
                         stream.eob_run += stream.ReadBits(n);
                     }
                     stream.eob_run--;
                     break;
                 }
                 i += 15;
             }
         }
     }
 }
Example #15
0
 public void DecodeDCFirst(JPEGBinaryReader stream, float[] dest)
 {
     float[] numArray = new float[0x40];
     int t = this.DCTable.Decode(stream);
     t = HuffmanTable.Extend(stream.ReadBits(t), t);
     t = ((int) this.previousDC) + t;
     this.previousDC = t;
     dest[0] = t << this.successiveLow;
 }
Example #16
0
        public void DecodeACFirst(JPEGBinaryReader stream, float[] zz)
        {
            if (stream.eob_run > 0)
            {
                stream.eob_run--;
                return;
            }

            for (int k = spectralStart; k <= spectralEnd; k++)
            {
                int s = ACTable.Decode(stream);
                int r = s >> 4;
                s &= 15;


                if (s != 0)
                {
                    k += r;

                    r = (int)stream.ReadBits(s);
                    s = (int)HuffmanTable.Extend(r, s);
                    zz[k] = s << successiveLow;
                }
                else
                {
                    if (r != 15)
                    {
                        stream.eob_run = 1 << r;
                        
                        if (r != 0)
                            stream.eob_run += stream.ReadBits(r);

                        stream.eob_run--;

                        break;
                    }

                    k += 15;
                }
            }
        }
Example #17
0
 public void DecodeDCRefine(JPEGBinaryReader stream, float[] dest)
 {
     if (stream.ReadBits(1) == 1)
     {
         dest[0] = (int)dest[0] | (1 << successiveLow);
     }
 }
        private void DecodeScan(byte numberOfComponents, byte[] componentSelector, int resetInterval, JPEGBinaryReader jpegReader, ref byte marker)
        {
            //TODO: not necessary
            jpegReader.eob_run = 0;

            int mcuIndex = 0;
            int mcuTotalIndex = 0;

            // This loops through until a MarkerTagFound exception is
            // found, if the marker tag is a RST (Restart Marker) it
            // simply skips it and moves on this system does not handle
            // corrupt data streams very well, it could be improved by
            // handling misplaced restart markers.

            int h = 0, v = 0;
            int x = 0;

            long lastPosition = jpegReader.BaseStream.Position;

            //TODO: replace this with a loop which knows how much data to expect
            while (true)
            {
                #region Inform caller of decode progress

                if (ProgressUpdateMethod != null)
                {
                    if (jpegReader.BaseStream.Position >= lastPosition + JpegDecoder.ProgressUpdateByteInterval)
                    {
                        lastPosition = jpegReader.BaseStream.Position;
                        ProgressUpdateMethod(lastPosition);
                    }
                }

                #endregion

                try
                {
                    // Loop though capturing MCU, instruct each
                    // component to read in its necessary count, for
                    // scaling factors the components automatically
                    // read in how much they need

                    // Sec A.2.2 from CCITT Rec. T.81 (1992 E)
                    bool interleaved = !(numberOfComponents == 1);

                    if (!interleaved)
                    {
                        JpegComponent comp = Scan.GetComponentById(componentSelector[0]);

                        comp.SetBlock(mcuIndex);

                        comp.DecodeMCU(jpegReader, h, v);

                        int mcus_per_line = mcus_per_row(comp);
                        int blocks_per_line = (int) Math.Ceiling((double)this.Width / (8 * comp.factorH));

                        // TODO: Explain the non-interleaved scan ------

                        h++; x++;

                        if (h == comp.factorH)
                        {
                            h = 0; mcuIndex++;
                        }

                        if( (x % mcus_per_line) == 0)
                        {
                            x = 0;
                            v++;

                            if (v == comp.factorV)
                            {
                                if (h != 0) { mcuIndex++; h = 0; }
                                v = 0;
                            }
                            else
                            {
                                mcuIndex -= blocks_per_line;

                                // we were mid-block
                                if (h != 0) { mcuIndex++; h = 0; }
                            }
                        }

                        // -----------------------------------------------

                    }
                    else // Components are interleaved
                    {
                        for (int compIndex = 0; compIndex < numberOfComponents; compIndex++)
                        {
                            JpegComponent comp = Scan.GetComponentById(componentSelector[compIndex]);
                            comp.SetBlock(mcuTotalIndex);

                            for (int j = 0; j < comp.factorV; j++)
                                for (int i = 0; i < comp.factorH; i++)
                                {
                                    comp.DecodeMCU(jpegReader, i, j);
                                }
                        }

                        mcuIndex++;
                        mcuTotalIndex++;
                    }
                }
                // We've found a marker, see if the marker is a restart
                // marker or just the next marker in the stream. If
                // it's the next marker in the stream break out of the
                // while loop, if it's just a restart marker skip it
                catch (JPEGMarkerFoundException ex)
                {
                    marker = ex.Marker;

                    // Handle JPEG Restart Markers, this is where the
                    // count of MCU's per interval is compared with
                    // the count actually obtained, if it's short then
                    // pad on some MCU's ONLY for components that are
                    // greater than one. Also restart the DC prediction
                    // to zero.
                    if (marker == JPEGMarker.RST0
                        || marker == JPEGMarker.RST1
                        || marker == JPEGMarker.RST2
                        || marker == JPEGMarker.RST3
                        || marker == JPEGMarker.RST4
                        || marker == JPEGMarker.RST5
                        || marker == JPEGMarker.RST6
                        || marker == JPEGMarker.RST7)
                    {
                        for (int compIndex = 0; compIndex < numberOfComponents; compIndex++)
                        {
                            JpegComponent comp = Scan.GetComponentById(componentSelector[compIndex]);
                            if (compIndex > 1)
                                comp.padMCU(mcuTotalIndex, resetInterval - mcuIndex);
                            comp.resetInterval();
                        }

                        mcuTotalIndex += (resetInterval - mcuIndex);
                        mcuIndex = 0;
                    }
                    else
                    {
                        break; // We're at the end of our scan, exit out.
                    }
                }
            }
        }
Example #19
0
 public void DecodeMCU(JPEGBinaryReader jpegReader, int i, int j)
 {
     Decode(jpegReader, scanMCUs[i,j]);
 }
        public void DecodeScanProgressive(byte successiveApproximation, byte startSpectralSelection, byte endSpectralSelection, 
                                          byte numberOfComponents, byte[] componentSelector, int resetInterval, JPEGBinaryReader jpegReader, ref byte marker)
        {
            byte successiveHigh = (byte)(successiveApproximation >> 4);
            byte successiveLow = (byte)(successiveApproximation & 0x0f);

            if ((startSpectralSelection > endSpectralSelection) || (endSpectralSelection > 63))
                throw new Exception("Bad spectral selection.");

            bool dcOnly = startSpectralSelection == 0;
            bool refinementScan = (successiveHigh != 0);

            if (dcOnly) // DC scan
            {
                if (endSpectralSelection != 0)
                    throw new Exception("Bad spectral selection for DC only scan.");
            }
            else // AC scan
            {
                if (numberOfComponents > 1)
                    throw new Exception("Too many components for AC scan!");
            }

            // Set the decode function for all the components
            // TODO: set this for the scan and let the component figure it out
            for (int compIndex = 0; compIndex < numberOfComponents; compIndex++)
            {
                JpegComponent comp = Scan.GetComponentById(componentSelector[compIndex]);

                comp.successiveLow = successiveLow;

                if (dcOnly)
                {
                    if (refinementScan) // DC refine
                        comp.Decode = comp.DecodeDCRefine;
                    else  //               DC first
                        comp.Decode = comp.DecodeDCFirst;
                }
                else
                {
                    comp.spectralStart = startSpectralSelection;
                    comp.spectralEnd = endSpectralSelection;

                    if (refinementScan) // AC refine
                        comp.Decode = comp.DecodeACRefine;
                    else  //               AC first
                        comp.Decode = comp.DecodeACFirst;
                }
            }

            DecodeScan(numberOfComponents, componentSelector, resetInterval, jpegReader, ref marker);
        }
Example #21
0
        /// <summary>
        /// Generated from text on F-23, F.13 - Huffman decoded of AC coefficients
        /// on ISO DIS 10918-1. Requirements and Guidelines.
        /// </summary>
        internal void decode_ac_coefficients(JPEGBinaryReader JPEGStream, float[] zz)
        {
            for (int k = 1; k < 64; k++)
            {
                int s = ACTable.Decode(JPEGStream);
                int r = s >> 4;
                s &= 15;


                if (s != 0)
                {
                    k += r;

                    r = (int)JPEGStream.ReadBits(s);
                    s = (int)HuffmanTable.Extend(r, s);
                    zz[k] = s;
                }
                else
                {
                    if (r != 15)
                    {
                        //throw new JPEGMarkerFoundException();
                        return;
                    }
                    k += 15;
                }
            }
        }
Example #22
0
 public void DecodeACRefine(JPEGBinaryReader stream, float[] dest)
 {
     int num = ((int) 1) << this.successiveLow;
     int num2 = ((int) (-1)) << this.successiveLow;
     int spectralStart = this.spectralStart;
     if (stream.eob_run == 0)
     {
         while (spectralStart <= this.spectralEnd)
         {
             int num4 = this.ACTable.Decode(stream);
             int n = num4 >> 4;
             num4 &= 15;
             if (num4 != 0)
             {
                 if (num4 != 1)
                 {
                     throw new Exception("Decode Error");
                 }
                 if (stream.ReadBits(1) == 1)
                 {
                     num4 = num;
                 }
                 else
                 {
                     num4 = num2;
                 }
             }
             else if (n != 15)
             {
                 stream.eob_run = ((int) 1) << n;
                 if (n > 0)
                 {
                     stream.eob_run += stream.ReadBits(n);
                 }
                 break;
             }
             do
             {
                 if (dest[spectralStart] != 0f)
                 {
                     if ((stream.ReadBits(1) == 1) && ((((int) dest[spectralStart]) & num) == 0))
                     {
                         if (dest[spectralStart] >= 0f)
                         {
                             dest[spectralStart] += num;
                         }
                         else
                         {
                             dest[spectralStart] += num2;
                         }
                     }
                 }
                 else if (--n < 0)
                 {
                     break;
                 }
                 spectralStart++;
             }
             while (spectralStart <= this.spectralEnd);
             if ((num4 != 0) && (spectralStart < 0x40))
             {
                 dest[spectralStart] = num4;
             }
             spectralStart++;
         }
     }
     if (stream.eob_run > 0)
     {
         while (spectralStart <= this.spectralEnd)
         {
             if (((dest[spectralStart] != 0f) && (stream.ReadBits(1) == 1)) && ((((int) dest[spectralStart]) & num) == 0))
             {
                 if (dest[spectralStart] >= 0f)
                 {
                     dest[spectralStart] += num;
                 }
                 else
                 {
                     dest[spectralStart] += num2;
                 }
             }
             spectralStart++;
         }
         stream.eob_run--;
     }
 }