Пример #1
0
 private void ProcessFrameHeader(ref JpegReader reader, bool metadataOnly, bool overrideAllowed)
 {
     // Read length field
     if (!reader.TryReadLength(out ushort length))
     {
         ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment length.");
         return;
     }
     if (!reader.TryReadBytes(length, out ReadOnlySequence <byte> buffer))
     {
         ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment content.");
         return;
     }
     if (!JpegFrameHeader.TryParse(buffer, metadataOnly, out JpegFrameHeader frameHeader, out int bytesConsumed))
     {
         ThrowInvalidDataException(reader.ConsumedByteCount - length + bytesConsumed, "Failed to parse frame header.");
         return;
     }
     if (!overrideAllowed && _frameHeader.HasValue)
     {
         ThrowInvalidDataException(reader.ConsumedByteCount, "Multiple frame is not supported.");
         return;
     }
     _frameHeader = frameHeader;
 }
Пример #2
0
 private void ProcessDefineQuantizationTable(ref JpegReader reader)
 {
     if (!reader.TryReadLength(out ushort length))
     {
         ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment length.");
         return;
     }
     if (!reader.TryReadBytes(length, out ReadOnlySequence <byte> buffer))
     {
         ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment content.");
         return;
     }
     ProcessDefineQuantizationTable(buffer, reader.ConsumedByteCount - length);
 }
Пример #3
0
        private static ReadOnlySequence <byte> CopyMarkerData(ref JpegReader reader, ref JpegWriter writer)
        {
            if (!reader.TryReadLength(out ushort length))
            {
                ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment length.");
            }

            if (!reader.TryReadBytes(length, out ReadOnlySequence <byte> buffer))
            {
                ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment content.");
            }
            writer.WriteLength(length);
            writer.WriteBytes(buffer);
            return(buffer);
        }
Пример #4
0
 private JpegScanHeader ProcessScanHeader(ref JpegReader reader, bool metadataOnly)
 {
     if (!reader.TryReadLength(out ushort length))
     {
         ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment length.");
     }
     if (!reader.TryReadBytes(length, out ReadOnlySequence <byte> buffer))
     {
         ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment content.");
     }
     if (!JpegScanHeader.TryParse(buffer, metadataOnly, out JpegScanHeader scanHeader, out int bytesConsumed))
     {
         ThrowInvalidDataException(reader.ConsumedByteCount - length + bytesConsumed, "Failed to parse scan header.");
     }
     return(scanHeader);
 }
Пример #5
0
        private void ProcessDefineRestartInterval(ref JpegReader reader)
        {
            if (!reader.TryReadLength(out ushort length))
            {
                ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment length.");
                return;
            }
            if (!reader.TryReadBytes(length, out ReadOnlySequence <byte> buffer) || buffer.Length < 2)
            {
                ThrowInvalidDataException(reader.ConsumedByteCount, "Unexpected end of input data when reading segment content.");
                return;
            }
            Span <byte> local = stackalloc byte[2];

            buffer.Slice(0, 2).CopyTo(local);
            _restartInterval = BinaryPrimitives.ReadUInt16BigEndian(local);
        }