public static JpegSegmentData ReadSegments(SequentialReader reader, Iterable<JpegSegmentType> segmentTypes)
		{
			// Must be big-endian
			System.Diagnostics.Debug.Assert((reader.IsMotorolaByteOrder()));
			// first two bytes should be JPEG magic number
			int magicNumber = reader.GetUInt16();
			if (magicNumber != unchecked((int)(0xFFD8)))
			{
				throw new JpegProcessingException("JPEG data is expected to begin with 0xFFD8 (ГїГ�) not 0x" + Sharpen.Extensions.ToHexString(magicNumber));
			}
			ICollection<sbyte> segmentTypeBytes = null;
			if (segmentTypes != null)
			{
				segmentTypeBytes = new HashSet<sbyte>();
				foreach (JpegSegmentType segmentType in segmentTypes)
				{
					segmentTypeBytes.Add(segmentType.byteValue);
				}
			}
			JpegSegmentData segmentData = new JpegSegmentData();
			do
			{
				// next byte is the segment identifier: 0xFF
				short segmentIdentifier = reader.GetUInt8();
				if (segmentIdentifier != unchecked((int)(0xFF)))
				{
					throw new JpegProcessingException("Expected JPEG segment start identifier 0xFF, not 0x" + Sharpen.Extensions.ToHexString(segmentIdentifier));
				}
				// next byte is the segment type
				sbyte segmentType = reader.GetInt8();
				if (segmentType == SegmentSos)
				{
					// The 'Start-Of-Scan' segment's length doesn't include the image data, instead would
					// have to search for the two bytes: 0xFF 0xD9 (EOI).
					// It comes last so simply return at this point
					return segmentData;
				}
				if (segmentType == MarkerEoi)
				{
					// the 'End-Of-Image' segment -- this should never be found in this fashion
					return segmentData;
				}
				// next 2-bytes are <segment-size>: [high-byte] [low-byte]
				int segmentLength = reader.GetUInt16();
				// segment length includes size bytes, so subtract two
				segmentLength -= 2;
				if (segmentLength < 0)
				{
					throw new JpegProcessingException("JPEG segment size would be less than zero");
				}
				// Check whether we are interested in this segment
				if (segmentTypeBytes == null || segmentTypeBytes.Contains(segmentType))
				{
					sbyte[] segmentBytes = reader.GetBytes(segmentLength);
					System.Diagnostics.Debug.Assert((segmentLength == segmentBytes.Length));
					segmentData.AddSegment(segmentType, segmentBytes);
				}
				else
				{
					// Some if the JPEG is truncated, just return what data we've already gathered
					if (!reader.TrySkip(segmentLength))
					{
						return segmentData;
					}
				}
			}
			while (true);
		}