public void getSOF(SOFInfo sof, UInt32 offset, UInt32 size) { if (!input.isValid(offset, size)) { throw new Exception("getSOF: Start offset plus size is longer than file. Truncated file."); } try { Endianness host_endian = Common.getHostEndianness(); // JPEG is big endian if (host_endian == Endianness.big) { input = new TIFFBinaryReader(input.BaseStream, offset, size); } else { input = new TIFFBinaryReaderRE(input.BaseStream, offset, size); } if (getNextMarker(false) != JpegMarker.M_SOI) { throw new Exception("getSOF: Image did not start with SOI. Probably not an LJPEG"); } while (true) { JpegMarker m = getNextMarker(true); if (JpegMarker.M_SOF3 == m) { parseSOF(sof); return; } if (JpegMarker.M_EOI == m) { throw new Exception("LJpegDecompressor: Could not locate Start of Frame."); } } } catch (IOException) { throw new Exception("LJpegDecompressor: IO exception, read outside file. Corrupt File."); } }
public void parseSOF(SOFInfo sof) { UInt32 headerLength = (uint)input.ReadInt16(); sof.prec = input.ReadByte(); sof.h = (uint)input.ReadInt16(); sof.w = (uint)input.ReadInt16(); sof.cps = input.ReadByte(); if (sof.prec > 16) { throw new Exception("LJpegDecompressor: More than 16 bits per channel is not supported."); } if (sof.cps > 4 || sof.cps < 1) { throw new Exception("LJpegDecompressor: Only from 1 to 4 components are supported."); } if (headerLength != 8 + sof.cps * 3) { throw new Exception("LJpegDecompressor: Header size mismatch."); } for (UInt32 i = 0; i < sof.cps; i++) { sof.compInfo[i].componentId = input.ReadByte(); UInt32 subs = input.ReadByte(); frame.compInfo[i].superV = subs & 0xf; frame.compInfo[i].superH = subs >> 4; UInt32 Tq = input.ReadByte(); if (Tq != 0) { throw new Exception("LJpegDecompressor: Quantized components not supported."); } } sof.initialized = true; }