public float[] GetBCH(BCH bch, bool debug = false) { Log.Information($"encoding BCH: [{bch.Text}]"); if (Log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) { var binary = bch.Binary.ToArray(); for (int i = 0; i < bch.Text.Length; i++) { var chunk = new string(binary.Skip(8 * i).Take(8).Select(x => x == 1 ? '1' : '0').ToArray()); var b = Convert.ToByte(chunk, 2); Log.Debug($"decoded BCH: {(char)b} ({string.Concat(chunk.Select(x => x.ToString()).ToArray())})"); } } var bitLength = 4; var one = Enumerable.Repeat(1f, bitLength).ToArray(); var zero = Enumerable.Repeat(-1f, bitLength).ToArray(); var bin = debug ? Enumerable.Range(0, bch.Binary.Count()).Select(x => x / 8 % 2 == 0 ? zero : one).SelectMany(x => x).ToArray() : bch.Binary.Select(x => x == 1 ? one : zero).SelectMany(x => x).ToArray(); var padding = new float[ImageWidth - bin.Length]; return(Enumerable.Range(0, bitLength).Select(_ => GetLine(bin.Concat(padding).ToArray())).SelectMany(x => x).ToArray()); }
private static void Send(string filename, BCH bch = null) { var fax = new Fax(16000); Log.Information($"loading: {filename}"); using var original = Image.FromFile(filename); Log.Information($"original image: {original.Width}x{original.Height}"); var scalingFactor = fax.ImageWidth / (float)original.Width; using var scaled = new Bitmap(fax.ImageWidth, (int)Math.Round(original.Height * scalingFactor)); using var graphics = Graphics.FromImage(scaled); graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.DrawImage(original, 0, 0, scaled.Width, scaled.Height); Log.Information($"scaled image: {scaled.Width}x{scaled.Height}"); var wavFilename = Path.Combine(Path.GetDirectoryName(filename), $"{Path.GetFileNameWithoutExtension(filename)}.wav"); using var writer = new WaveFileWriter(wavFilename, new WaveFormat(fax.SampleRate, 1)); var start = fax.GetStart(); writer.WriteSamples(start, 0, start.Length); var phasing = fax.GetPhasing(); writer.WriteSamples(phasing, 0, phasing.Length); if (bch != null) { var header = fax.GetBCH(bch); writer.WriteSamples(header, 0, header.Length); //var debug = fax.GetBCH(bch, true); //writer.WriteSamples(debug, 0, debug.Length); } for (int y = 0; y < scaled.Height; y++) { var pixels = Enumerable.Range(0, scaled.Width).Select(x => scaled.GetPixel(x, y).GetBrightness() * 2f - 1).ToArray(); var samples = fax.GetLine(pixels); writer.WriteSamples(samples, 0, samples.Length); } var stop = fax.GetStop(); writer.WriteSamples(stop, 0, stop.Length); Log.Information($"wrote: {wavFilename}"); }