/// <summary>Figure F.16 - Reads the huffman code bit-by-bit.</summary> public JpegReadStatusInt Decode(JpegBinaryReader jpegStream) { int i = 0; var readState = jpegStream.ReadBits(1); if (readState.Status != Status.Success) { return(readState); } short code = (short)readState.Result; while (code > maxcode[i]) { i++; code <<= 1; readState = jpegStream.ReadBits(1); if (readState.Status != Status.Success) { return(readState); } code |= (short)readState.Result; } readState.Result = huffval[code + (valptr[i])]; if (readState.Result < 0) { readState.Result = 256 + readState.Result; } readState.Status = Status.Success; return(readState); }
public JpegReadStatusInt DecodeACFirst(JpegBinaryReader stream, float[] zz) { if (stream.eobRun > 0) { stream.eobRun--; return(JpegReadStatusInt.GetSuccess()); } for (int k = spectralStart; k <= spectralEnd; k++) { var status = ACTable.Decode(stream); if (status.Status != Status.Success) { return(status); } int s = status.Result; int r = s >> 4; s &= 15; if (s != 0) { k += r; status = stream.ReadBits(s); if (status.Status != Status.Success) { return(status); } r = status.Result; s = HuffmanTable.Extend(r, s); zz[k] = s << successiveLow; } else { if (r != 15) { stream.eobRun = 1 << r; if (r != 0) { status = stream.ReadBits(r); if (status.Status != Status.Success) { return(status); } stream.eobRun += status.Result; } stream.eobRun--; break; } k += 15; } } return(JpegReadStatusInt.GetSuccess()); }
public JpegReadStatusInt DecodeDCRefine(JpegBinaryReader stream, float[] dest) { var status = stream.ReadBits(1); if (status.Status != Status.Success) { return(status); } if (status.Result == 1) { dest[0] = (int)dest[0] | (1 << successiveLow); } return(JpegReadStatusInt.GetSuccess()); }
/// <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> /// <param name="diff">The DC coefficient</param> /// <returns>The result of the read</returns> public JpegReadStatusInt decode_dc_coefficient(JpegBinaryReader jpegStream, out float diff) { diff = 0; var status = DCTable.Decode(jpegStream); if (status.Status != Status.Success) { return(status); } int t = status.Result; status = jpegStream.ReadBits(t); if (status.Status != Status.Success) { return(status); } diff = HuffmanTable.Extend(status.Result, t); diff = (previousDC + diff); previousDC = diff; return(status); }
/// <summary> /// Generated from text on F-23, F.13 - Huffman decoded of AC coefficients /// on ISO DIS 10918-1. Requirements and Guidelines. /// </summary> internal JpegReadStatusInt decode_ac_coefficients(JpegBinaryReader jpegStream, float[] zz) { var status = new JpegReadStatusInt(); for (int k = 1; k < 64; k++) { status = ACTable.Decode(jpegStream); if (status.Status != Status.Success) { return(status); } int s = status.Result; int r = s >> 4; s &= 15; if (s != 0) { k += r; status = jpegStream.ReadBits(s); if (status.Status != Status.Success) { return(status); } s = HuffmanTable.Extend(status.Result, s); zz[k] = s; } else { if (r != 15) { //throw new JPEGMarkerFoundException(); return(status); } k += 15; } } return(status); }
public JpegReadStatusInt DecodeDCFirst(JpegBinaryReader stream, float[] dest) { var status = DCTable.Decode(stream); if (status.Status != Status.Success) { return(status); // We reached the end of the stream. } int s = status.Result; status = stream.ReadBits(s); if (status.Status != Status.Success) { return(status); } int r = status.Result; s = HuffmanTable.Extend(r, s); s = (int)previousDC + s; previousDC = s; dest[0] = s << successiveLow; return(status); }
public JpegReadStatusInt DecodeACRefine(JpegBinaryReader stream, float[] dest) { int p1 = 1 << successiveLow; int m1 = (-1) << successiveLow; int k = spectralStart; if (stream.eobRun == 0) { for (; k <= spectralEnd; k++) { #region Decode and check S var status = ACTable.Decode(stream); if (status.Status != Status.Success) { return(status); } int s = status.Result; int r = s >> 4; s &= 15; if (s != 0) { if (s != 1) { throw new Exception("Decode Error"); } status = stream.ReadBits(1); if (status.Status != Status.Success) { return(status); } s = status.Result == 1 ? p1 : m1; } else { if (r != 15) { stream.eobRun = 1 << r; if (r > 0) { status = stream.ReadBits(r); if (status.Status != Status.Success) { return(status); } stream.eobRun += status.Result; } break; } } // if (s != 0) #endregion // Apply the update do { if (dest[k] != 0) { status = stream.ReadBits(1); if (status.Status != Status.Success) { return(status); } if (status.Result == 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.eobRun > 0) { for (; k <= spectralEnd; k++) { if (dest[k] != 0) { var status = stream.ReadBits(1); if (status.Status != Status.Success) { return(status); } if (status.Result == 1) { if (((int)dest[k] & p1) == 0) { if (dest[k] >= 0) { dest[k] += p1; } else { dest[k] += m1; } } } } } stream.eobRun--; } return(JpegReadStatusInt.GetSuccess()); }