private void buttonEncode_Click(object sender, EventArgs e) { try { int key; if (!int.TryParse(textBoxGetEncodeKey.Text, out key)) { throw new Exception("Ошибка при вводе ключа!"); } EncodeKey = Int32.Parse(textBoxGetEncodeKey.Text); SteganoTransformation transformation = new SteganoTransformation(); SteganoContainer container = new SteganoContainer(ContainerForEncode); SteganoMessage message = new SteganoMessage(InputMessage); container.InitBlocks(8, 8); container.InitBlueComponent(8, 8); transformation.Encode(container, message, EncodeKey, 250); container.InsertBlueComponent(8, 8); container.SaveImage(8, 8, "encode_output.bmp"); MessageBox.Show("Успешно!\nФайл находится в директории:\n" + Directory.GetCurrentDirectory(), "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception excptn) { MessageBox.Show("Анта бака!\nОшибка при кодировании!\n" + excptn.Message + excptn.StackTrace, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public Color getMaxDifference(SteganoContainer original, SteganoContainer container) { // need to add different check params of containers if (container.Image.Height != original.Image.Height || container.Image.Width != original.Image.Width) { throw new Exception("Размеры изображений не совпадают, сэмпай!"); } byte g = Convert.ToByte(0); byte b = Convert.ToByte(0); byte r = Convert.ToByte(0); byte a = Convert.ToByte(0); for (var row = 0; row < container.Image.Height; row++) { for (var column = 0; column < container.Image.Width; column++) { Color pixelOriginal = original.Image.GetPixel((int)column, (int)row); Color pixelContainer = container.Image.GetPixel((int)column, (int)row); byte gBuf = (byte)(pixelOriginal.G ^ pixelContainer.G); byte bBuf = (byte)(pixelOriginal.B ^ pixelContainer.B); byte rBuf = (byte)(pixelOriginal.R ^ pixelContainer.R); byte aBuf = (byte)(pixelOriginal.A ^ pixelContainer.A); if (g < gBuf) { g = gBuf; } if (b < bBuf) { b = bBuf; } if (r < rBuf) { r = rBuf; } if (a < aBuf) { a = aBuf; } } } Color outputData = Color.FromArgb(a, r, g, b); return(outputData); }
private void buttonСalculate_Click(object sender, EventArgs e) { try { StatisticalCharacteristics statParams = new StatisticalCharacteristics(); SteganoContainer container = new SteganoContainer(_mainForm.ContainerForDecode); SteganoContainer original = new SteganoContainer(_mainForm.ContainerForEncode); Color maxDifference = statParams.getMaxDifference(container, original); string maxDifferenceAlpha = Convert.ToString(maxDifference.A, 2).PadLeft(8, '0'); string maxDifferenceRed = Convert.ToString(maxDifference.R, 2).PadLeft(8, '0'); string maxDifferenceGreen = Convert.ToString(maxDifference.G, 2).PadLeft(8, '0'); string maxDifferenceBlue = Convert.ToString(maxDifference.B, 2).PadLeft(8, '0'); textBoxMaxDifference.Text = "Alpha: " + maxDifferenceAlpha + " Red: " + maxDifferenceRed + " Green: " + maxDifferenceGreen + " Blue: " + maxDifferenceBlue; double [] pValues = new double [300]; for (double i = 0; i < 300; i++) { pValues[(int)i] = i; } var psnrValues = statParams.getPSNRValues(_mainForm.ContainerForEncode, _mainForm.InputMessage, pValues.Length, _mainForm.EncodeKey); for (int i = 0; i < 300; i++) { if (double.IsInfinity(psnrValues[i])) { psnrValues[i] = 0; } } this.chartPSNRfromP.Series[0].Points.Clear(); for (int i = 0; i < pValues.Length; i++) { this.chartPSNRfromP.Series[0].Points.AddXY(pValues[i], psnrValues[i]); } } catch (Exception excptn) { MessageBox.Show("Анта бака!\nОшибка при расчете характеристик!\n" + excptn.Message + excptn.StackTrace, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public double [] getPSNRValues(string original, string message, int maxP, int key) { double [] PSNRvalues = new double[maxP]; Parallel.For(0, maxP, ctr => { SteganoTransformation transformation = new SteganoTransformation(); SteganoContainer originalImage = new SteganoContainer(original); SteganoContainer container = new SteganoContainer(original); SteganoMessage originalMessage = new SteganoMessage(message); container.InitBlocks(8, 8); container.InitBlueComponent(8, 8); transformation.Encode(container, originalMessage, key, ctr); container.InsertBlueComponent(8, 8); container.SaveBitmap(8, 8); double buf = getPSNR(originalImage, container); PSNRvalues[ctr] = buf; container.Image.Dispose(); originalImage.Image.Dispose(); }); return(PSNRvalues); }
public double getPSNR(SteganoContainer original, SteganoContainer container) { double mseR = 0; double mseG = 0; double mseB = 0; // Calculate mean square error of R, G, B. for (var row = 0; row < container.Image.Height; row++) { for (var column = 0; column < container.Image.Width; column++) { mseR += Math.Pow(original.Image.GetPixel(column, (int)row).R ^ container.Image.GetPixel(column, (int)row).R, 2); mseG += Math.Pow(original.Image.GetPixel(column, (int)row).G ^ container.Image.GetPixel(column, (int)row).G, 2); mseB += Math.Pow(original.Image.GetPixel(column, (int)row).B ^ container.Image.GetPixel(column, (int)row).B, 2); } } // Calculate final value of mean square error double MSE = (mseR + mseG + mseB) / ((container.Image.Height * container.Image.Width) * 3); // Calculate peak signal to noise ratio double PSNR = 10 * Math.Log10(Math.Pow(RGBMaxValue, 2) / MSE); return(PSNR); }
public void Encode(SteganoContainer container, SteganoMessage steganoMessage, int key, int P) { if (steganoMessage.Size + 32 > container.Blocks.Count) { throw new Exception("Размер контейнера слишком мал, сэмпай!"); } //Применили дкп ко всем блокам 8х8 foreach (var block in container.BlueComponent) { EncodeCoefficient.Add(DKP.Dkp(block)); } Random rnd = new Random(key); bool[] data = new bool[container.BlueComponent.Count - 32]; for (int i = 0; i < steganoMessage.DataBinaryString.Length; i++) { data[i] = true; } var distribution = Shuffle(data, rnd); var steganoMessageLength = GetBinaryStringFromUInt32(steganoMessage.Size); // put data about message length in blocks for (int counter = 0; counter < 32; counter++) { EncodeCoefficient[counter] = DKP.CoefficientChange(EncodeCoefficient[counter], steganoMessageLength[counter], u1, v1, u2, v2, P); } int marker = 32; for (int i = 32; i < container.BlueComponent.Count; i++) { if (marker == steganoMessage.DataBinaryString.Length + 32) { break; } if (distribution[i - 32] == true) { EncodeCoefficient[i] = DKP.CoefficientChange(EncodeCoefficient[i], steganoMessage.DataBinaryString[marker - 32], u1, v1, u2, v2, P); marker += 1; } else { continue; } } foreach (var block in EncodeCoefficient) { EncodeCoefficientForWrite.Add(DKP.Odkp(block)); } // change BlueComponent in container for (var counter = 0; counter < container.BlueComponent.Count; counter++) { byte[,] buf = new byte[8, 8]; for (uint i = 0; i < 8; i++) { for (uint k = 0; k < 8; k++) { if (EncodeCoefficientForWrite[counter][i, k] < 0) { buf[i, k] = 0; } else if (EncodeCoefficientForWrite[counter][i, k] > 255) { buf[i, k] = 255; } else { buf[i, k] = (byte)Math.Round(EncodeCoefficientForWrite[counter][i, k]); } } } container.BlueComponent[counter] = buf; } }
public void Decode(SteganoContainer container, SteganoMessage steganoMessage, int key) { // Применили ДКП к блокам 8х8 foreach (var block in container.BlueComponent) { DecodeCoefficient.Add(DKP.Dkp(block)); } double Abs1; double Abs2; string bits = ""; //временная переменная для записи сообщения double[,] dkp_8x8; // временная переменная для одной матрицы 8на8 var steganoMessageLength = ""; for (int counter = 0; counter < 32; counter++) { dkp_8x8 = DecodeCoefficient[counter]; Abs1 = Math.Abs(dkp_8x8[u1, v1]); Abs2 = Math.Abs(dkp_8x8[u2, v2]); if (Abs1 > Abs2) { steganoMessageLength += "0"; } if (Abs1 < Abs2) { steganoMessageLength += "1"; } } var steganoMessageLengthUInt32 = GetUInt32FromBinaryString(steganoMessageLength); if (steganoMessageLengthUInt32 + 32 > container.Blocks.Count) { throw new Exception("Контейнер поврежден, сэмпай!"); } Random rnd = new Random(key); bool[] data = new bool[container.BlueComponent.Count - 32]; for (int i = 0; i < steganoMessageLengthUInt32; i++) { data[i] = true; } var distribution = Shuffle(data, rnd); for (int k = 32; k < container.Blocks.Count; k++) { if (distribution[k - 32] == true) { dkp_8x8 = DecodeCoefficient[k]; Abs1 = Math.Abs(dkp_8x8[u1, v1]); Abs2 = Math.Abs(dkp_8x8[u2, v2]); if (Abs1 > Abs2) { bits += "0"; } if (Abs1 < Abs2) { bits += "1"; } } else { continue; } } steganoMessage.DataBinaryString = bits; }