public static bool TryParse(ReadOnlySequence <byte> buffer, bool metadataOnly, out JpegFrameHeader frameHeader, out int bytesConsumed) { if (buffer.IsSingleSegment) { return(TryParse(buffer.First.Span, metadataOnly, out frameHeader, out bytesConsumed)); } bytesConsumed = 0; if (buffer.Length < 6) { frameHeader = default; return(false); } Span <byte> local = stackalloc byte[6]; buffer.Slice(0, 6).CopyTo(local); byte numberOfComponenets = local[5]; ushort samplesPerLine = (ushort)(local[4] | (local[3] << 8)); ushort numberOfLines = (ushort)(local[2] | (local[1] << 8)); byte precision = local[0]; buffer = buffer.Slice(6); bytesConsumed += 6; if (buffer.Length < 3 * numberOfComponenets) { frameHeader = default; return(false); } if (metadataOnly) { bytesConsumed += 3 * numberOfComponenets; frameHeader = new JpegFrameHeader(precision, numberOfLines, samplesPerLine, numberOfComponenets, null); return(true); } JpegFrameComponentSpecificationParameters[] components = new JpegFrameComponentSpecificationParameters[numberOfComponenets]; for (int i = 0; i < components.Length; i++) { if (!JpegFrameComponentSpecificationParameters.TryParse(buffer, out components[i])) { frameHeader = default; return(false); } buffer = buffer.Slice(3); bytesConsumed += 3; } frameHeader = new JpegFrameHeader(precision, numberOfLines, samplesPerLine, numberOfComponenets, components); return(true); }
public static bool TryParse(ReadOnlySpan <byte> buffer, bool metadataOnly, out JpegFrameHeader frameHeader, out int bytesConsumed) { bytesConsumed = 0; if (buffer.Length < 6) { frameHeader = default; return(false); } byte numberOfComponenets = buffer[5]; ushort samplesPerLine = (ushort)(buffer[4] | (buffer[3] << 8)); ushort numberOfLines = (ushort)(buffer[2] | (buffer[1] << 8)); byte precision = buffer[0]; buffer = buffer.Slice(6); bytesConsumed += 6; if (buffer.Length < 3 * numberOfComponenets) { frameHeader = default; return(false); } if (metadataOnly) { bytesConsumed += 3 * numberOfComponenets; frameHeader = new JpegFrameHeader(precision, numberOfLines, samplesPerLine, numberOfComponenets, null); return(true); } JpegFrameComponentSpecificationParameters[] components = new JpegFrameComponentSpecificationParameters[numberOfComponenets]; for (int i = 0; i < components.Length; i++) { if (!JpegFrameComponentSpecificationParameters.TryParse(buffer, out components[i])) { frameHeader = default; return(false); } buffer = buffer.Slice(3); bytesConsumed += 3; } frameHeader = new JpegFrameHeader(precision, numberOfLines, samplesPerLine, numberOfComponenets, components); return(true); }