protected void SaveAsBrl4(string path, Rectangle area, string newExt, DataProcessor.Abstract.DataStringProcessor processor) { using (var fr = System.IO.File.Open(_file.Properties.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var fname = Path.ChangeExtension(path, newExt); using (var fw = System.IO.File.Open(fname, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) { fr.Seek(Marshal.SizeOf(typeof(RlViewer.Headers.Concrete.Rl4.Rl4RliFileHeader)), SeekOrigin.Begin); var rlSubHeader = _head.HeaderStruct.rlParams .ChangeImgDimensions(area.Width, area.Height) .ChangeFragmentShift(area.X, area.Y); RlViewer.Headers.Concrete.Rl4.Rl4RliFileHeader rl4Header = new Headers.Concrete.Rl4.Rl4RliFileHeader(_head.HeaderStruct.fileSign, _head.HeaderStruct.fileVersion, _head.HeaderStruct.rhgParams, rlSubHeader, _head.HeaderStruct.synthParams, _head.HeaderStruct.reserved); var brl4Head = rl4Header.ToBrl4(0, 1, 0); fw.Write(RlViewer.Behaviors.Converters.StructIO.WriteStruct<RlViewer.Headers.Concrete.Brl4.Brl4RliFileHeader>(brl4Head), 0, Marshal.SizeOf(brl4Head)); byte[] strHeader = new byte[SourceFile.Header.StrHeaderLength]; int strDataLength = _file.Width * _file.Header.BytesPerSample; byte[] frameData = new byte[area.Width * _file.Header.BytesPerSample]; long lineToStartSaving = (long)area.Y * (long)(_file.Width * _file.Header.BytesPerSample + SourceFile.Header.StrHeaderLength); long sampleToStartSaving = area.X * _file.Header.BytesPerSample; fr.Seek(lineToStartSaving, SeekOrigin.Current); for (int i = 0; i < area.Height; i++) { //read-write string header OnProgressReport((int)((double)i / (double)area.Height * 100)); if (OnCancelWorker()) { return; } fr.Read(strHeader, 0, SourceFile.Header.StrHeaderLength); var brl4StrHead = Converters.FileHeaderConverters.ToBrl4StrHeader(strHeader); fw.Write(brl4StrHead, 0, SourceFile.Header.StrHeaderLength); fr.Seek(sampleToStartSaving, SeekOrigin.Current); fr.Read(frameData, 0, frameData.Length); var processedFrame = processor.ProcessRawDataString(frameData); fw.Write(processedFrame, 0, processedFrame.Length); fr.Seek(strDataLength - frameData.Length - sampleToStartSaving, SeekOrigin.Current); } } } }