/// <summary> /// Writes the aSegmentData to a aFile. /// </summary> /// <param name="aFileName">where to write the information</param> /// <param name="aSegmentData">what to write in the aFile</param> public static void ToFile(string aFileName, JpegSegmentData aSegmentData) { BinaryFormatter lcBinFor = new BinaryFormatter(); try { using (var lcFileStream = new FileStream(aFileName, FileMode.CreateNew)) { lcBinFor.Serialize(lcFileStream, aSegmentData); } } catch (Exception) { } }
/// <summary> /// Writes the aSegmentData to a aFile. /// </summary> /// <param name="aFileName">where to write the information</param> /// <param name="aSegmentData">what to write in the aFile</param> public static void ToFile(string aFileName, JpegSegmentData aSegmentData) { FileStream lcFileStream = null; try { lcFileStream = new FileStream(aFileName, FileMode.CreateNew); BinaryFormatter lcBinFor = new BinaryFormatter(); lcBinFor.Serialize(lcFileStream, aSegmentData); } finally { if (lcFileStream != null) { lcFileStream.Close(); lcFileStream.Dispose(); } } }
/// <summary> /// Loads a jpegsegmentdata from a file. /// </summary> /// <param name="aFileName">where to find data</param> /// <returns>the jpegsegment asked</returns> public static JpegSegmentData FromFile(string aFileName) { BinaryFormatter lcBinFor = new BinaryFormatter(); JpegSegmentData res = null; try { using (var lcFileStream = new FileStream(aFileName, FileMode.Open)) { res = (JpegSegmentData)lcBinFor.Deserialize(lcFileStream); } } catch (Exception e) { throw new JpegProcessingException( "IOException processing Jpeg file: " + e.Message, e); } return(res); }
/// <summary> /// Reads segments /// </summary> /// <exception cref="JpegProcessingException">for any problems processing the Jpeg data</exception> private void ReadSegments() { this.segmentDataMap = new JpegSegmentData(); BufferedStream lcInStream = this.GetJpegInputStream(); try { int lcOffset = 0; // first two bytes should be jpeg magic number if (!this.IsValidJpegHeaderBytes(lcInStream)) { throw new JpegProcessingException("not a jpeg file"); } lcOffset += 2; do { // next byte is 0xFF byte lcSegmentIdentifier = (byte)(lcInStream.ReadByte() & 0xFF); if ((lcSegmentIdentifier & 0xFF) != 0xFF) { throw new JpegProcessingException( "expected jpeg segment start identifier 0xFF at offset " + lcOffset + ", not 0x" + (lcSegmentIdentifier & 0xFF).ToString("X")); } lcOffset++; // next byte is <segment-marker> byte lcSegmentMarker = (byte)(lcInStream.ReadByte() & 0xFF); lcOffset++; // next 2-bytes are <segment-size>: [high-byte] [low-byte] byte[] lcSegmentLengthBytes = new byte[2]; lcInStream.Read(lcSegmentLengthBytes, 0, 2); lcOffset += 2; int lcSegmentLength = ((lcSegmentLengthBytes[0] << 8) & 0xFF00) | (lcSegmentLengthBytes[1] & 0xFF); // segment length includes size bytes, so subtract two lcSegmentLength -= 2; if (lcSegmentLength > (lcInStream.Length - lcInStream.Position)) { throw new JpegProcessingException("segment size would extend beyond file stream length"); } else if (lcSegmentLength < 0) { throw new JpegProcessingException("segment size would be less than zero"); } byte[] lcSegmentBytes = new byte[lcSegmentLength]; lcInStream.Read(lcSegmentBytes, 0, lcSegmentLength); lcOffset += lcSegmentLength; if ((lcSegmentMarker & 0xFF) == (SEGMENT_SOS & 0xFF)) { // The 'Start-Of-Scan' segment'str 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; } else if ((lcSegmentMarker & 0xFF) == (MARKER_EOI & 0xFF)) { // the 'End-Of-Image' segment -- this should never be found in this fashion return; } else { this.segmentDataMap.AddSegment(lcSegmentMarker, lcSegmentBytes); } // didn't find the one we're looking for, loop through to the next segment } while (true); } catch (IOException ioe) { //throw new JpegProcessingException("IOException processing Jpeg aFile", ioe); throw new JpegProcessingException( "IOException processing Jpeg file: " + ioe.Message, ioe); } finally { if (lcInStream != null) { lcInStream.Close(); lcInStream.Dispose(); } } }
/// <summary> /// Reads segments /// </summary> /// <exception cref="JpegProcessingException">for any problems processing the Jpeg data</exception> private void ReadSegments() { this.segmentDataMap = new JpegSegmentData(); using (var lcInStream = this.GetJpegInputStream()) { try { int lcOffset = 0; // first two bytes should be jpeg magic number if (!this.IsValidJpegHeaderBytes(lcInStream)) { throw new JpegProcessingException("not a jpeg file"); } lcOffset += 2; do { // next byte is 0xFF byte lcSegmentIdentifier = (byte)(lcInStream.ReadByte() & 0xFF); if ((lcSegmentIdentifier & 0xFF) != 0xFF) { throw new JpegProcessingException( "expected jpeg segment start identifier 0xFF at offset " + lcOffset + ", not 0x" + (lcSegmentIdentifier & 0xFF).ToString("X")); } lcOffset++; // next byte is <segment-marker> byte lcSegmentMarker = (byte)(lcInStream.ReadByte() & 0xFF); lcOffset++; // next 2-bytes are <segment-size>: [high-byte] [low-byte] byte[] lcSegmentLengthBytes = new byte[2]; lcInStream.Read(lcSegmentLengthBytes, 0, 2); lcOffset += 2; int lcSegmentLength = ((lcSegmentLengthBytes[0] << 8) & 0xFF00) | (lcSegmentLengthBytes[1] & 0xFF); // segment length includes size bytes, so subtract two lcSegmentLength -= 2; if (lcSegmentLength > (lcInStream.Length - lcInStream.Position)) { throw new JpegProcessingException("segment size would extend beyond file stream length"); } else if (lcSegmentLength < 0) { throw new JpegProcessingException("segment size would be less than zero"); } byte[] lcSegmentBytes = new byte[lcSegmentLength]; lcInStream.Read(lcSegmentBytes, 0, lcSegmentLength); lcOffset += lcSegmentLength; if ((lcSegmentMarker & 0xFF) == (SEGMENT_SOS & 0xFF)) { // The 'Start-Of-Scan' segment'str 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; } else if ((lcSegmentMarker & 0xFF) == (MARKER_EOI & 0xFF)) { // the 'End-Of-Image' segment -- this should never be found in this fashion return; } else { this.segmentDataMap.AddSegment(lcSegmentMarker, lcSegmentBytes); } // didn't find the one we're looking for, loop through to the next segment } while (true); } catch (Exception ioe) { throw new JpegProcessingException( "IOException processing Jpeg file: " + ioe.Message, ioe); } } }