public void TestBinaryWatermark() { BinaryWatermark mark = new BinaryWatermark(new byte[] { 1, 2, 3, 4 }); Watermarker.EmbedWatermark(file, mark, "password", "results/BinaryMark.png"); PNGFile file2 = new PNGFile("results/BinaryMark.png"); BinaryWatermark extract = (BinaryWatermark)Watermarker.ExtractWatermark(file2, "password"); Expect(extract.data, Is.EqualTo(new byte[] { 1, 2, 3, 4 })); }
public void TestLongRSWatermark() { byte[] data = new byte[300]; for (var x = 0; x < 300; x++) { data[x] = (byte)(x % 17); } BinaryWatermark binMark = new BinaryWatermark(data); Watermarker.ReedSolomonProtection = true; Watermarker.EmbedWatermark(file, binMark, "password", "results/LongRS.png"); PNGFile file2 = new PNGFile("results/LongRS.png"); BinaryWatermark binMark2 = Watermarker.ExtractWatermark <BinaryWatermark>(file2, "password"); for (var x = 0; x < binMark2.data.Length; x++) { Expect(binMark2.data[x], Is.EqualTo(binMark.data[x])); } }
/// <summary> /// Extracts a stored watermark from a PNGFile. /// </summary> /// <param name="file">PNGFile that contains the watermark.</param> /// <param name="mark">An empty watermark that will be populated.</param> /// <param name="password">Password that was used to embed the watermark.</param> /// <returns></returns> public static Watermark ExtractWatermark(PNGFile file, string password) { Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, new byte[] { 112, 52, 63, 42, 180, 121, 53, 27 }, 1000); PNGScrambler scrambler = new PNGScrambler(file, bytes.GetBytes(16), bytes.GetBytes(8)); byte[] data = null; byte markType; if (ReedSolomonProtection == false) { byte[] type = ReadBytes(file, scrambler, 1); markType = type[0]; if (markType > 9) { return(null); } byte[] dword = ReadBytes(file, scrambler, 4, 1); int length = BitConverter.ToInt32(dword, 0); try { data = ReadBytes(file, scrambler, length, 5); } catch (Exception e) { return(null); } } else { byte[] rsType = ReadBytes(file, scrambler, 3, 0); byte[] type = correctErrors(rsType, 1); markType = type[0]; if ((markType & 0x80) != 0x80) { return(null); } markType ^= 0x80; byte[] rsLength = ReadBytes(file, scrambler, 12, 3); byte[] length = correctErrors(rsLength, 4); int markLength = BitConverter.ToInt32(length, 0); int position = 0; int numBlocks = (markLength / 256) + (markLength % 256 > 0 ? 1 : 0); MemoryStream msOut = new MemoryStream(); while (numBlocks > 0) { int bytesInBlock = (markLength - (position / 2) < 256 ? markLength - (position / 2) : 256); bytesInBlock *= 2; byte[] codeBytes = ReadBytes(file, scrambler, bytesInBlock, position + 15); byte[] fixedData = correctErrors(codeBytes, bytesInBlock / 2); msOut.Write(fixedData, 0, fixedData.Length); numBlocks--; position += bytesInBlock; } data = msOut.ToArray(); } Watermark mark = null; switch (markType) { case 1: mark = TextWatermark.LoadFromBytes(data); break; case 2: mark = FileWatermark.LoadFromBytes(data); break; case 3: mark = BinaryWatermark.LoadFromBytes(data); break; case 4: mark = CompositeWatermark.LoadFromBytes(data); break; case 9: mark = EncryptedWatermark.LoadFromBytes(data); break; } return(mark); }