public static List <Bitmap> Decode(MemoryStream stream) { var reader = new BinaryReader(stream); var images = new List <Bitmap>(); stream.Seek(0, SeekOrigin.Begin); bool eof = false; for (int image = 1; ; image++) { try { var imgInfo = new ImgInfo(); while (true) { while (reader.ReadByte() != 0xff) { ; } int markerId = reader.ReadByte(); switch ((Markers)markerId) { case Markers.App0: App0.Read(reader, imgInfo); break; case Markers.App14: App14.Read(reader, imgInfo); break; case Markers.Dqt: Dqt.Read(reader, imgInfo); break; case Markers.Sof0: Sof0.Read(reader, imgInfo); break; case Markers.Sof2: Sof2.Read(reader, imgInfo); break; case Markers.Dht: Dht.Read(reader, imgInfo); break; case Markers.Sos: images.Add(Sos.Read(reader, imgInfo)); break; case Markers.Soi: imgInfo = new ImgInfo(); //Logger.Write("Start of Image " + image); //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X")); imgInfo.startOfImageFound = true; break; case Markers.Dri: Dri.Read(reader, imgInfo); break; case Markers.Eoi: //Logger.Write("End of Image " + image); //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X")); eof = true; break; // Unknown markers, or markers used outside of their specified area default: Default.Read(reader, imgInfo, (Markers)markerId); break; } if (eof) { eof = false; break; } } } catch (EndOfStreamException) { break; } catch (Exception ex) { //Logger.WriteLine(ex.Message); } } reader.Close(); //Logger.Flush(); return(images); }
public static List <Image> Decode(Stream stream) { var reader = new BinaryReader(stream); var images = new List <Image>(); stream.Seek(0, SeekOrigin.Begin); var imgInfo = new ImgInfo(); for (long length = stream.Length; ;) { int markerId; do { if (stream.Position == length) { goto end; } } while (reader.ReadByte() != 0xff); markerId = reader.ReadByte(); switch ((Markers)markerId) { case Markers.App0: App0.Read(reader, imgInfo); break; case Markers.App14: App14.Read(reader, imgInfo); break; case Markers.Dqt: Dqt.Read(reader, imgInfo); break; case Markers.Sof0: Sof0.Read(reader, imgInfo); break; case Markers.Sof2: Sof2.Read(reader, imgInfo); break; case Markers.Dht: Dht.Read(reader, imgInfo); break; case Markers.Sos: images.Add(Sos.Read(reader, imgInfo)); break; case Markers.Soi: //Logger.Write("Start of Image " + image); //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X")); imgInfo = new ImgInfo { startOfImageFound = true }; break; case Markers.Dri: Dri.Read(reader, imgInfo); break; case Markers.Eoi: //Logger.Write("End of Image " + image); //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X")); //eof = true; break; // Unknown markers, or markers used outside of their specified area default: Default.Read(reader, imgInfo, (Markers)markerId); break; } } end: reader.Dispose(); return(images); }