internal static int NextMcuPos(ImgInfo imgInfo, BitReader bReader, int mcu, int numMcusX, int numMcusY) { // If we are expecting a restart marker, find it in the stream, // reset the DC prediction variables and calculate the new MCU position // otherwise, just increment the position by one if (imgInfo.hasRestartMarkers && (mcu % imgInfo.restartInterval) == imgInfo.restartInterval - 1 && (mcu < numMcusX * numMcusY - 1)) { Pixz.Markers currRestMarker = bReader.SyncStreamToNextRestartMarker(); if (currRestMarker == Pixz.Markers.Eoi) // If EOI marker was found { return(numMcusX * numMcusY); // Early terminate the decoding process } int difference = currRestMarker - imgInfo.prevRestMarker; if (difference <= 0) { difference += Markers.Dri.RestartMarkerPeriod; } // For non corrupted images, difference should be always 1 ResetDeltas(imgInfo); imgInfo.mcuStrip += difference; imgInfo.prevRestMarker = currRestMarker; return(imgInfo.mcuStrip * imgInfo.restartInterval); } if (bReader.WasEoiFound()) { return(numMcusX * numMcusY); } return(++mcu); }
public static void Read(BinaryReader reader, ImgInfo imgInfo, Pixz.Markers markerId) { /*Logger.Write("Unknown marker (" + markerId.ToString("X") + ")"); * * if (!imgInfo.startOfImageFound) * { * Logger.Write(" found outside of image"); * } * * Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));*/ // Check if marker is not followed by a length argument if (markerId >= Pixz.Markers.Rs0 && markerId <= Pixz.Markers.Rs7) { return; } if (markerId == Pixz.Markers.LiteralFF) { return; } if (!imgInfo.startOfImageFound) { return; } ushort length = reader.ReadBEUInt16(); //Logger.WriteLine("Length: " + length.ToString()); reader.BaseStream.Seek(length - 2, SeekOrigin.Current); }
/// <summary> /// Read a byte from the stream, taking into account when markers are found /// If we find a marker, lock the stream at that current position /// and return zeros on the next reads so we can at least decode part of /// the image (Happens when the file is corrupted) /// </summary> /// <returns>A byte read from the current stream</returns> protected byte ReadByteOrMarker() { if (!lockReading) { byte number = reader.ReadByte(); if (number == 0xff) // Marker found { byte markerValue = reader.ReadByte(); if (markerValue == 0x00) // 0xff00 is interpreted as a 0xff value { return(number); } else { lastReadMarker = (Pixz.Markers)markerValue; lockReading = true; return(0); } } else // Not a marker, just return read value { return(number); } } else { return(0); } }
/// <summary> /// Deletes all data in the buffer, without rewinding all readahead bytes /// in stream /// </summary> protected void Flush() { availableBits = 0; readData = 0; lastReadMarker = 0; lockReading = false; }
/// <summary> /// Finds the next restart marker, or the EOI marker in the stream /// </summary> /// <returns>The next restart marker</returns> internal Pixz.Markers SyncStreamToNextRestartMarker() { // When decoding actual image pixel data, ignore all markers except // restart markers, or EOI marker while (!((lastReadMarker >= Pixz.Markers.Rs0 && lastReadMarker <= Pixz.Markers.Rs7) || lastReadMarker == Pixz.Markers.Eoi)) { ReadByteOrMarker(); } Pixz.Markers tempMarker = lastReadMarker; Flush(); return(tempMarker); }
/// <summary> /// Read a byte from the stream, taking into account when markers are found /// If we find a marker, lock the stream at that current position /// and return zeros on the next reads so we can at least decode part of /// the image (Happens when the file is corrupted) /// </summary> /// <returns>A byte read from the current stream</returns> protected byte ReadByteOrMarker() { if (!lockReading) { byte number = reader.ReadByte(); if (number == 0xff) // Marker found { byte markerValue = reader.ReadByte(); if (markerValue == 0x00) // 0xff00 is interpreted as a 0xff value { return number; } else { lastReadMarker = (Pixz.Markers)markerValue; lockReading = true; return 0; } } else // Not a marker, just return read value { return number; } } else { return 0; } }