// Методы public IonogramReader(string fname) { this._filename = fname; try { // создаем объект BinaryReader using (BinaryReader reader = new BinaryReader(File.Open(fname, FileMode.Open))) { _contentInfo = getContentInfo(reader); _ver = reader.ReadUInt32(); switch (_ver) { case 0: // ИПГ _header = readHeader2(reader); _image_o = new Bitmap( (int)_header.count_freq, (int)_header.count_height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); _image_x = new Bitmap(_image_o); fillImage(reader); break; case 1: // грязная ионограмма as is _header = readHeader2(reader); _image_o = new Bitmap( (int)_header.count_freq, (int)_header.count_height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); fillDirtyImage(reader); //_image_x = new Bitmap(_image_o); // дублируем рисунок break; default: throw new ArgumentOutOfRangeException("Неизвестная версия формата файла ионограмм."); } } } catch (Exception e) { string caption = "Ошибка при чтении файла " + this._filename + '.'; DialogResult result = MessageBox.Show(e.Message, caption, MessageBoxButtons.OK); } }
protected contentInfo getContentInfo(BinaryReader reader) { contentInfo _out; int i, k; byte[] samples; ushort[] hugeUShortArray = null; // Инициализация _out.min_o = 255; _out.min_x = 255; _out.max_o = 0; _out.max_x = 0; _out.read_error = false; _out.values = hugeUShortArray; // Сохраним положение в файле. long oldPos = reader.BaseStream.Position; try { // Читаем заголовок. _ver = reader.ReadUInt32(); if (_ver >= 0 && _ver <= 2) { _header = readHeader2(reader); } else { throw new ArgumentOutOfRangeException("Неизвестная версия формата файла ионограмм."); } int BufferSize = (int)this._header.count_height * (int)this._header.count_freq; hugeUShortArray = new ushort[BufferSize]; bool file_eof = false; switch (_ver) { case 0: // ИПГ // Пробегаем по данным. FrequencyData curFrq; SignalResponse curSignal; // пока не достигнут конец файла считываем каждое значение из файла do { curFrq = readFrequencyData(reader); for (i = 0; i < curFrq.count_o; i++) // о-отражения { curSignal = readSignalResponse(reader); samples = reader.ReadBytes(curSignal.count_samples); for (k = 0; k < curSignal.count_samples; k++) { _out.min_o = (_out.min_o < samples[k]) ? _out.min_o : samples[k]; _out.max_o = (_out.max_o > samples[k]) ? _out.max_o : samples[k]; } } for (i = 0; i < curFrq.count_x; i++) // о-отражения { curSignal = readSignalResponse(reader); samples = reader.ReadBytes(curSignal.count_samples); for (k = 0; k < curSignal.count_samples; k++) { _out.min_x = (_out.min_x < samples[k]) ? _out.min_x : samples[k]; _out.max_x = (_out.max_x > samples[k]) ? _out.max_x : samples[k]; } } file_eof = reader.BaseStream.Position >= reader.BaseStream.Length; }while (!file_eof); // если возникает ошибка - файл битый break; case 1: // грязная ионограмма // превести uint к ushort и вместо 4х поставить 2 samples = reader.ReadBytes(2 * BufferSize); Buffer.BlockCopy(samples, 0, hugeUShortArray, 0, 2 * BufferSize); ushort sample; for (i = 0; i < BufferSize; i++) { sample = hugeUShortArray[i]; _out.min_o = (_out.min_o < sample) ? _out.min_o : sample; _out.max_o = (_out.max_o > sample) ? _out.max_o : sample; } _out.min_x = _out.min_o; _out.max_x = _out.max_o; _out.values = hugeUShortArray; break; } } catch (IOException e) { // Ошибку не обрабатываем. Что нашли в файле - то нашли. _out.read_error = true; } // Возвратим исходное положение в файле. reader.BaseStream.Seek(oldPos, SeekOrigin.Begin); return(_out); }