Exemple #1
0
        public void GetSwitches_Test() {
            Vertex
                v1 = new Vertex(48, 50, 2, 4),
                v2 = new Vertex(49, 48, 2, 4),
                v3 = new Vertex(54, 53, 2, 4),
                v4 = new Vertex(50, 51, 3, 4),
                v5 = new Vertex(50, 49, 2, 4),
                v6 = new Vertex(65, 66, 2, 4),
                v7 = new Vertex(97, 98, 3, 4),
                v8 = new Vertex(96, 35, 2, 4),
                v9 = new Vertex(95, 26, 2, 4);

            Edge
                e1 = new Edge(v2, v3, 5, true, true),
                e2 = new Edge(v2, v5, 1, true, true),
                e3 = new Edge(v2, v6, 17, true, false),
                e4 = new Edge(v2, v3, 5, false, false),
                e5 = new Edge(v2, v5, 1, false, false),
                e7 = new Edge(v8, v9, 1, true, true),
                e6 = new Edge(v2, v6, 17, false, true),
                e8 = new Edge(v8, v9, 9, false, false);

            Graph g = new Graph() {
                Vertices = {v1, v2, v3, v4, v5, v6, v7, v8, v9},
                Edges = {e1, e2, e3, e4, e5, e6, e7, e8}
            };

            Assert.AreEqual(new List<Edge> {e5,e7}.ToString(), g.GetSwitches().ToString() );
        }
Exemple #2
0
        public void GraphToString_Test()
        {
            short SampleInput1 = 5;
            short SampleInput2 = 6;
            byte messageInput1 = 11;
            byte modulo1 = 100;

            Vertex vertex1 = new Vertex(SampleInput1, SampleInput2, messageInput1, modulo1);

            short SampleInput3 = 7;
            short SampleInput4 = 8;
            byte messageInput2 = 100;
            byte modulo2 = 100;

            Vertex vertex2 = new Vertex(SampleInput3, SampleInput4, messageInput2, modulo2);

            Graph graph1 = new Graph();

            graph1.Vertices.Add(vertex1);
            graph1.Vertices.Add(vertex2);

            Assert.AreEqual("These are my vertices: \n(5,6)\n(7,8)\n", graph1.ToString());
        }
Exemple #3
0
        public void MergeGraphAndQuantizedValues_Test()
        {
            JpegImage ji = new JpegImage(new Bitmap(200, 100), 100, 4);
            PrivateObject po = new PrivateObject(ji);
            
            Vertex
                v1 = new Vertex(2, 3, 1, 4);

            Graph inputGraph = new Graph();

            inputGraph.Vertices.Add(v1);
            short[,] inBlock8 = new short[8, 8];
            
            var expectedTuple = new Tuple<short[,], HuffmanTable, HuffmanTable, int>(inBlock8, ji.YDCHuffman, ji.YACHuffman, 0);
            
            var quantizedBlocks = new List<Tuple<short[,], HuffmanTable, HuffmanTable, int>>();
            var expectedQuantizedBlocks = new List<Tuple<short[,], HuffmanTable, HuffmanTable, int>>();

            quantizedBlocks.Add(expectedTuple);

            expectedQuantizedBlocks = quantizedBlocks;

            po.Invoke("_mergeGraphAndQuantizedValues", inputGraph);

            var returnedQuantizedBlocks = (List<Tuple<short[,], HuffmanTable, HuffmanTable, int>>)po.GetField("_quantizedBlocks");

            Assert.AreEqual(expectedQuantizedBlocks.ToString(), returnedQuantizedBlocks.ToString());
        }
Exemple #4
0
        public void RefactorGraph_Test() //TODO: fix this test
        {
            PrivateType pt = new PrivateType(typeof(JpegImage));

            Vertex
                v1 = new Vertex(0, 1, 0, 4),
                v2 = new Vertex(2, 3, 2, 4),
                v3 = new Vertex(4, 5, 3, 4),
                v4 = new Vertex(6, 7, 3, 4),
                v5 = new Vertex(8, 9, 4, 4),
                v6 = new Vertex(10, 11, 5, 4);
            Edge
                e1 = new Edge(v1, v2, 1, true, false),
                e2 = new Edge(v3, v4, 2, true, true),
                e3 = new Edge(v5, v6, 2, false, true);

            Graph inputGraph = new Graph();
            Graph sad = new Graph();
            inputGraph.Edges.Add(e1);
            inputGraph.Edges.Add(e2);
            inputGraph.Edges.Add(e3);
            sad.Edges.Add(e1);
            sad.Edges.Add(e2);
            sad.Edges.Add(e3);
            sad.Vertices.AddRange(new List<Vertex>() { v1, v2, v3, v4, v5, v6 });
            inputGraph.Vertices.AddRange(new List<Vertex>() { v1, v2, v3, v4, v5, v6 });

            
            pt.InvokeStatic("_refactorGraph", inputGraph);
            
            Assert.AreEqual(sad.Vertices, inputGraph.Vertices);
        }
Exemple #5
0
        public void AddEdge_Test()
        {
            PrivateType pt = new PrivateType(typeof(JpegImage));

            Vertex
                v1 = new Vertex(0, 1, 0, 4),
                v2 = new Vertex(2, 3, 2, 4),
                v3 = new Vertex(4, 5, 3, 4),
                v4 = new Vertex(6, 7, 3, 4),
                v5 = new Vertex(8, 9, 4, 4),
                v6 = new Vertex(10, 11, 5, 4);

            Graph inputGraph = new Graph();
            inputGraph.Vertices.AddRange(new List<Vertex>() { v1, v2, v3, v4, v5, v6 });

            int inputThreshold = 5;

            pt.InvokeStatic("_addEdge", new object[] {true, false, v1, v2, inputThreshold, inputGraph});    //pass
            pt.InvokeStatic("_addEdge", new object[] {true, true, v3, v4, inputThreshold, inputGraph});     //pass
            pt.InvokeStatic("_addEdge", new object[] {false, true, v5, v6, inputThreshold, inputGraph});    //fail

            Graph expectedGraph = new Graph();

            expectedGraph.Edges.Add(new Edge(v1, v2, 3, true, false));
            expectedGraph.Edges.Add(new Edge(v3, v4, 2, true, true));

            Assert.AreEqual(expectedGraph.Edges, inputGraph.Edges);
        }
Exemple #6
0
        public void AddVertices_Test()
        {
            PrivateObject po = new PrivateObject(new JpegImage(new Bitmap(200, 100), 100, 4));

            List<short> inputNonZeroValues = new List<short>();
            List<byte> inputMessage = new List<byte>();

            for (int i = 0; i < 20; i++)
            {
                inputNonZeroValues.Add((short)i);
            }
            for (int e = 0; e < 10; e++)
            {
                inputMessage.Add((byte)e);
            }

            po.SetField("_nonZeroValues", inputNonZeroValues);
            po.SetField("_message",inputMessage);

            Vertex
                v1 = new Vertex(0, 1, 0, 4),
                v2 = new Vertex(2, 3, 1, 4),
                v3 = new Vertex(4, 5, 2, 4),
                v4 = new Vertex(6, 7, 3, 4),
                v5 = new Vertex(8, 9, 4, 4),
                v6 = new Vertex(10, 11, 5, 4),
                v7 = new Vertex(12, 13, 6, 4),
                v8 = new Vertex(14, 15, 7, 4),
                v9 = new Vertex(16, 17, 8, 4),
                v10 = new Vertex(18, 19, 9, 4);

            Graph returnedGraph = new Graph();
            Graph expectedGraph = new Graph();

            expectedGraph.Vertices.AddRange(new List<Vertex>() {v1, v2, v3, v4, v5, v6, v7, v8, v9, v10});

            po.Invoke("_addVertices", returnedGraph);

            Assert.AreEqual(expectedGraph.ToString(), returnedGraph.ToString());
        }
Exemple #7
0
        private void _mergeGraphAndQuantizedValues(Graph graph) {
            int vertexPos = 0;
            bool firstValue = true;
            int numberOfVertices = graph.Vertices.Count;
            int len = _quantizedBlocks.Count * 64;
            for (int i = 0; i < len; i++) {
                if (vertexPos >= numberOfVertices) {
                    break;
                }
                int array = i / 64;
                int xpos = i % 8;
                int ypos = (i % 64) / 8;

                if (xpos + ypos != 0 && _quantizedBlocks[array].Item1[xpos, ypos] != 0) {
                    if (firstValue) {
                        _quantizedBlocks[array].Item1[xpos, ypos] = graph.Vertices[vertexPos].SampleValue1;
                        firstValue = false;
                    } else {
                        _quantizedBlocks[array].Item1[xpos, ypos] = graph.Vertices[vertexPos].SampleValue2;
                        firstValue = true;
                        vertexPos++;
                    }
                }
            }
        }
Exemple #8
0
        private void _refactorGraph(Graph graph) {
            _s.Restart();
            List<Edge> chosen = graph.GetSwitches();
            _s.Stop();
            _setTimings("GetSwitches", _s.ElapsedMilliseconds);

            foreach (Edge edge in chosen) {
                _swapVertexData(edge);
            }

            foreach (Vertex vertex in graph.Vertices) {
                if ((vertex.SampleValue1 + vertex.SampleValue2).Mod(vertex.Modulo) != vertex.Message) {
                    _forceSampleChange(vertex);
                }
            }
        }
Exemple #9
0
 private static void _addEdge(bool firstFirst, bool secondFirst, Vertex first, Vertex second, int threshold, Graph g) {
     if (((firstFirst ? first.SampleValue2 : first.SampleValue1) + (secondFirst ? second.SampleValue1 : second.SampleValue2)).Mod(first.Modulo) == first.Message) {
         if (((firstFirst ? first.SampleValue1 : first.SampleValue2) + (secondFirst ? second.SampleValue2 : second.SampleValue1)).Mod(second.Modulo) == second.Message) {
             Edge e = new Edge(first, second, firstFirst, secondFirst);
             if (e.Weight < threshold) {
                 g.Edges.Add(e);
             }
         }
     }
 }
Exemple #10
0
        private void _encodeMessage() {
            Graph graph = new Graph();

            for (int i = 0; i < 15; i += 2) {
                graph.Vertices.Add(new Vertex(_nonZeroValues[i], _nonZeroValues[i + 1], _message[0], 4));
                _message.RemoveAt(0);
            }

            int valuesNeeded = _message.Count * 16 / (int)Math.Log(M, 2) - 1;
            for (int i = 16; i < valuesNeeded; i += 2) {
                if (_message.Count == 0) {
                    break;
                }
                graph.Vertices.Add(new Vertex(_nonZeroValues[i], _nonZeroValues[i + 1], _message[0], M));
                _message.RemoveAt(0);
            }

            _s.Restart();
            int threshold = 5;
            foreach (Vertex currentVertex in graph.Vertices) {
                foreach (Vertex otherVertex in graph.Vertices.Where(otherVertex => currentVertex != otherVertex)) {
                    _addEdge(true, true, currentVertex, otherVertex, threshold, graph);
                    _addEdge(true, false, currentVertex, otherVertex, threshold, graph);
                    _addEdge(false, true, currentVertex, otherVertex, threshold, graph);
                    _addEdge(false, false, currentVertex, otherVertex, threshold, graph);
                }
            }
            _s.Stop();
            _setTimings("Adding edges", _s.ElapsedMilliseconds);

            //Swap values and force the rest
            _refactorGraph(graph);

            //Put the changed values back into the QuantizedValues
            _mergeGraphAndQuantizedValues(graph);
        }
Exemple #11
0
        private static void _refactorGraph(Graph graph) {
            List<Edge> chosen = graph.GetSwitches();

            foreach (Edge edge in chosen) {
                _swapVertexData(edge);
            }

            foreach (Vertex vertex in graph.Vertices) {
                if ((vertex.SampleValue1 + vertex.SampleValue2).Mod(vertex.Modulo) != vertex.Message) {
                    _forceSampleChange(vertex);
                }
            }
        }
Exemple #12
0
 private static void _addEdge(bool startFirst, bool endFirst, Vertex first, Vertex second, int threshold, Graph g) {
     
     short weight = (short)Math.Abs((startFirst ? first.SampleValue1 : first.SampleValue2) - (endFirst ? second.SampleValue1 : second.SampleValue2));
     if (weight < threshold) {
         if (((startFirst ? first.SampleValue2 : first.SampleValue1) + (endFirst ? second.SampleValue1 : second.SampleValue2)).Mod(first.Modulo) == first.Message) {
             if (((startFirst ? first.SampleValue1 : first.SampleValue2) + (endFirst ? second.SampleValue2 : second.SampleValue1)).Mod(second.Modulo) == second.Message) {
                 lock (g) {
                     g.Edges.Add(new Edge(first, second, weight, startFirst, endFirst));
                 }
             }
         }
    }
 }
Exemple #13
0
 private void _addVertices(Graph g) {
     for (int i = 0; i < 15; i += 2) {
         g.Vertices.Add(new Vertex(_nonZeroValues[i], _nonZeroValues[i + 1], _message[0], 4));
         _message.RemoveAt(0);
     }
     int j;
     for (j = 16; _message.Any(); j += 2) {
         g.Vertices.Add(new Vertex(_nonZeroValues[j], _nonZeroValues[j + 1], _message[0], M));
         _message.RemoveAt(0);
     }
 }
Exemple #14
0
        private void _encodeMessage() {
            Graph graph = new Graph();

            //Add vertices for each part of the message
            _addVertices(graph);

            const int threshold = 5;
            //Find alle the possible switches between vertices and add them as edges
            List<Vertex> toBeChanged = graph.Vertices.Where(x => (x.SampleValue1 + x.SampleValue2).Mod(x.Modulo) != x.Message).ToList();
            int length = toBeChanged.Count;
            Parallel.For(0, length, i => {
                for (int j = i + 1; j < length; j++) {
                    _addEdge(true, true, toBeChanged[i], toBeChanged[j], threshold, graph);
                    _addEdge(true, false, toBeChanged[i], toBeChanged[j], threshold, graph);
                    _addEdge(false, true, toBeChanged[i], toBeChanged[j], threshold, graph);
                    _addEdge(false, false, toBeChanged[i], toBeChanged[j], threshold, graph);
                }
            });

            //Swap values and force the rest
            _refactorGraph(graph);

            //Put the changed values back into the QuantizedValues
            _mergeGraphAndQuantizedValues(graph);
        }
Exemple #15
0
        private void _encodeMessage() {
            Graph graph = new Graph();

            //Add vertices for each part of the message
            _addVertices(graph);

            _s.Restart();
            List<Vertex> toBeChanged = graph.Vertices.Where(x => (x.SampleValue1 + x.SampleValue2).Mod(x.Modulo) != x.Message).ToList();
            int length = toBeChanged.Count;
            int threshold = 5;
            Parallel.For(0, length, i => {
                for (int j = i + 1; j < length; j++) {
                    _addEdge(true, true, toBeChanged[i], toBeChanged[j], threshold, graph);
                    _addEdge(true, false, toBeChanged[i], toBeChanged[j], threshold, graph);
                    _addEdge(false, true, toBeChanged[i], toBeChanged[j], threshold, graph);
                    _addEdge(false, false, toBeChanged[i], toBeChanged[j], threshold, graph);
                }
            });
            _s.Stop();
            _setTimings("Adding edges", _s.ElapsedMilliseconds);

            //Swap values and force the rest
            _refactorGraph(graph);

            //Put the changed values back into the QuantizedValues
            _mergeGraphAndQuantizedValues(graph);
        }