Ejemplo n.º 1
0
        /// <summary>
        /// When "Send" button is clicked, a new thread is run not to block the main thread.
        /// "Send" button is disabled until the thread finished its job.
        /// Input image is converted to vectors, which are sent through the channel with provided
        /// error probability p. Image vectors are sent two ways - encoded and later decoded after receiving them
        /// from the channel and not encoded.
        /// </summary>
        private void SendImageBtn_Click(object sender, EventArgs e)
        {
            panel.SendBtn.Enabled = false;
            new Thread(() =>
            {
                var p = panel.P;

                // Convert image to vectors and separate the header (it is not sent through the channel)
                var bitmapToVector     = new Vector(ConversionUtils.ConvertBitmapToIntArray(UploadedImage));
                var bitmapHeader       = bitmapToVector.Data.Take(54 * 8);
                var bitmapVectorToSend = new Vector(bitmapToVector.Data.Skip(54 * 8).ToArray());

                try
                {
                    // Encode input image vectors
                    var encodedVector = Encoder.EncodeBinarySequence(bitmapVectorToSend, M, out int additionalBits);

                    // Send (not)encoded vectors through the channel
                    var receivedNotEncoded = Channel.SendBinaryMessage(bitmapVectorToSend, p, out _);
                    var receivedEncoded    = Channel.SendBinaryMessage(encodedVector, p, out _);

                    // Decode received encoded vectors
                    var decoded = Decoder.DecodeBinarySequence(receivedEncoded, M);

                    DeconvertedNotEncoded = ConversionUtils.ConvertIntArrayToImage(bitmapHeader.Concat(receivedNotEncoded.Data).ToArray());
                    DeconvertedEncoded    = ConversionUtils.ConvertIntArrayToImage(bitmapHeader.Concat(decoded.Data).ToArray(), additionalBits);
                }
                // Handling 'out of memory' failure
                catch (OutOfMemoryException)
                {
                    MessageBox.Show("System is out of memory!");
                    return;
                }
                // There can be cases where corrupted Bitmap is provided and cannot be reconverted to the image
                catch
                {
                    MessageBox.Show("Corrupted Bitmap has been provided.");
                    this.BeginInvoke((Action)(() =>
                    {
                        panel.SendBtn.Enabled = true;
                        return;
                    }));
                }

                this.BeginInvoke((Action)(() =>
                {
                    notEncodedPicture.Image = DeconvertedNotEncoded;
                    encodedPicture.Image = DeconvertedEncoded;
                    panel.SendBtn.Enabled = true;
                    this.ChangeReceivedControlsVisibility(true);
                }));
            }).Start();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// When "Send" button is clicked, a new thread is run not to block the main thread.
        /// "Send" button is disabled until the thread finished its job.
        /// Input text is converted to vectors, which are sent through the channel with provided
        /// error probability p. Text is sent two ways - encoded and later decoded after receiving it
        /// from the channel and not encoded.
        /// </summary>
        private void SendBtn_Click(object sender, EventArgs e)
        {
            probabilityPanel.SendBtn.Enabled = false;
            new Thread(() =>
            {
                // If no text is provided, cancel further processes
                if (messageBox.Text.Length == 0)
                {
                    BeginInvoke((Action)(() =>
                    {
                        encodedTextBox.Text = "";
                        notEncodedTextBox.Text = "";
                        probabilityPanel.SendBtn.Enabled = true;
                        return;
                    }));
                }

                var p               = probabilityPanel.P;
                var textToBinary    = ConversionUtils.CovertStringToBinaryArray(messageBox.Text);
                var inputTextVector = new Vector(textToBinary);

                // Handling 'out of memory' failure
                try
                {
                    // Ecode input text vectors
                    var encodedTextVectors = Encoder.EncodeBinarySequence(inputTextVector, M, out int additionalBitCount);

                    // Sent (not)encoded text vectors through the channel
                    var receivedEncodedTextVector    = Channel.SendBinaryMessage(encodedTextVectors, p, out _);
                    var receivedNotEncodedTextVector = Channel.SendBinaryMessage(inputTextVector, p, out _);

                    // Decoded received encoded text vector
                    var decodedText = Decoder.DecodeBinarySequence(receivedEncodedTextVector, M);

                    SentEncodedResult    = ConversionUtils.ConvertBinaryArrayToString(decodedText.Data, additionalBitCount);
                    SentNotEncodedResult = ConversionUtils.ConvertBinaryArrayToString(receivedNotEncodedTextVector.Data, 0);
                }
                catch (OutOfMemoryException)
                {
                    MessageBox.Show("System is out of memory!");
                    return;
                }

                BeginInvoke((Action)(() => {
                    encodedTextBox.Text = SentEncodedResult;
                    notEncodedTextBox.Text = SentNotEncodedResult;
                    probabilityPanel.SendBtn.Enabled = true;
                }));
            }).Start();
        }