using System.Collections.Generic; using OpenCvSharp; using Com.Lmc.ShuiYin.One.Converter; using Com.Lmc.ShuiYin.One.Util; namespace Com.Lmc.ShuiYin.One.Dencoder { public abstract class Encoder { protected Converter.Converter converter; // protected for subclasses public Encoder(Converter.Converter converter) { this.converter = converter; } public Converter.Converter GetConverter() { return converter; } public void SetConverter(Converter.Converter converter) { this.converter = converter; } public void Encode(string image, string watermark, string output) { Mat src = Utils.Read(image, 1); // CV_8S -> Color (1) List channel = new List(); List newChannel = new List(); Cv2.Split(src, out Mat[] splitChannel); channel.AddRange(splitChannel); // Java: for (int i = 0; i < 3; i++) // OpenCvSharp Split returns array of Mats. // Ensure we have 3 channels? The loop is hardcoded to 3. for (int i = 0; i < 3 && i < channel.Count; i++) { Mat com = this.converter.Start(channel[i]).Clone(); this.AddWatermark(com, watermark); this.converter.Inverse(com); // In Java: newChannel.add(i, com); newChannel.Add(com); } // If src has more than 3 channels (e.g. alpha), we should probably keep them? // The java code only loops 3 times. if (channel.Count > 3) { for (int i = 3; i < channel.Count; i++) { newChannel.Add(channel[i]); } } Mat res = new Mat(); Cv2.Merge(newChannel.ToArray(), res); if (res.Rows != src.Rows || res.Cols != src.Cols) { res = new Mat(res, new Rect(0, 0, src.Width, src.Height)); } Cv2.ImWrite(output, res); } public abstract void AddWatermark(Mat com, string watermark); } }