Пример #1
0
        internal string ReadText(ChannelUsage usage, bool returnFilteredText = true, bool useThreads = true)
        {
            var values = new bool[usage.GetTotaleUsage() * _inputData.Size];
            var usages = usage.GetUsages();

            var numThreads   = useThreads ? _inputData.Size >> _threadSizeModifier | 1 : 1;
            var threadLength = _inputData.Size / numThreads;

            var threads = new Thread[numThreads];

            for (var i = 0; i < numThreads; i++)
            {
                var reader = new ReaderThread(_inputData, usages, values, i * threadLength, threadLength);
                threads[i] = new Thread(reader.ReadBooleans);
                threads[i].Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }

            var bytes = BitOperators.BitsToBytes(values);
            var text  = Encoding.UTF8.GetString(bytes);

            return(returnFilteredText ? FindOriginalText(text) : text);
        }
Пример #2
0
 /// <summary>
 /// Decrypts a text from a BitmapImage
 /// </summary>
 /// <param name="picture">The picture in which a text will be searched for</param>
 /// <param name="usage">How many bits will be used in each channel</param>
 /// <param name="seed">The seed used in the generator for the order of pixels</param>
 /// <returns>Text found in the picture</returns>
 public static string Decrypt(BitmapImage picture, ChannelUsage usage, int seed)
 {
     using (var decrypter = new Decrypter(new Picture(picture.ToByteArray(), seed, picture.PixelHeight, picture.PixelWidth)))
     {
         var text = decrypter.ReadText(usage);
         return((text == "") ? "No text found." : text);
     }
 }
Пример #3
0
        public static int GetMaxCharCount(int width, int height, ChannelUsage usage)
        {
            const int startAndEndLength = 12;
            var       pixCount          = height * width;
            var       channelUsage      = usage.GetTotaleUsage();

            return(pixCount * channelUsage - startAndEndLength);
        }
Пример #4
0
 public static Bitmap Encrypt(Bitmap picture, ChannelUsage usage, int seed, string text)
 {
     using (var encrypter = new Encrypter(new Picture(picture, seed)))
     {
         encrypter.InsertText(text, usage);
         return(encrypter.GetPictureData().GetImage());
     }
 }
Пример #5
0
 public static string Decrypt(Bitmap encryptedPicture, ChannelUsage usage, int seed)
 {
     using (encryptedPicture)
         using (var decrypter = new Decrypter(new Picture(encryptedPicture, seed)))
         {
             var text = decrypter.ReadText(usage);
             return((text == "") ? "No text found." : text);
         }
 }
Пример #6
0
 private void OnGUI()
 {
     r = EditorGUILayout.ObjectField("R Channel Texture: ", r, typeof(Texture2D), false) as Texture2D;
     rTextureChannel = (ChannelUsage)EditorGUILayout.EnumPopup("R Texture Channel: ", rTextureChannel);
     g = EditorGUILayout.ObjectField("G Channel Texture: ", g, typeof(Texture2D), false) as Texture2D;
     gTextureChannel = (ChannelUsage)EditorGUILayout.EnumPopup("G Texture Channel: ", gTextureChannel);
     b = EditorGUILayout.ObjectField("B Channel Texture: ", b, typeof(Texture2D), false) as Texture2D;
     bTextureChannel = (ChannelUsage)EditorGUILayout.EnumPopup("B Texture Channel: ", bTextureChannel);
     a = EditorGUILayout.ObjectField("A Channel Texture: ", a, typeof(Texture2D), false) as Texture2D;
     aTextureChannel = (ChannelUsage)EditorGUILayout.EnumPopup("A Texture Channel: ", aTextureChannel);
     size            = EditorGUILayout.Vector2IntField("Texture target Size", size);
     path            = EditorGUILayout.TextField("Path: ", path);
     srgb            = EditorGUILayout.Toggle("SRGB", srgb);
     if (GUILayout.Button("Combine Texture"))
     {
         if (r && !r.isReadable)
         {
             Debug.LogError("You have to set R channel Texture as readable!");
             return;
         }
         if (g && !g.isReadable)
         {
             Debug.LogError("You have to set G channel Texture as readable!");
             return;
         }
         if (b && !b.isReadable)
         {
             Debug.LogError("You have to set B channel Texture as readable!");
             return;
         }
         if (a && !a.isReadable)
         {
             Debug.LogError("You have to set A channel Texture as readable!");
             return;
         }
         Texture2D resultTex = new Texture2D(size.x, size.y, TextureFormat.ARGB32, false, !srgb);
         for (int x = 0; x < size.x; ++x)
         {
             for (int y = 0; y < size.y; ++y)
             {
                 Color cl = new Color
                 {
                     r = r ? GetColor(rTextureChannel, r.GetPixelBilinear((x + 0.5f) / size.x, (y + 0.5f) / size.y)) : 0,
                     g = g ? GetColor(gTextureChannel, g.GetPixelBilinear((x + 0.5f) / size.x, (y + 0.5f) / size.y)) : 0,
                     b = b ? GetColor(bTextureChannel, b.GetPixelBilinear((x + 0.5f) / size.x, (y + 0.5f) / size.y)) : 0,
                     a = a ? GetColor(aTextureChannel, a.GetPixelBilinear((x + 0.5f) / size.x, (y + 0.5f) / size.y)) : 0
                 };
                 resultTex.SetPixel(x, y, cl);
             }
         }
         resultTex.Apply();
         System.IO.File.WriteAllBytes(path, resultTex.EncodeToPNG());
     }
 }
Пример #7
0
        public void TestProcessHuge()
        {
            var text  = "Pretty long text just to test if everything is working properly! I'll therefore add some more text so the whole process takes some more time which makes it more suitable to compare the performance as well as changes to it. This is a 4k picture. This could theoretically take some time but we'll manage to process this extremely fast";
            var image = ColoredBitmap.CreateBitmap(3840, 2160);
            var usage = new ChannelUsage(2);

            var pic     = Interface.Encrypt(image, usage, 0, text);
            var resText = Interface.Decrypt(pic, usage, 0);

            Assert.AreEqual(text, resText);
        }
Пример #8
0
        public void TestProcess()
        {
            const string text  = "Pretty long text just to test if everything is working properly!";
            var          image = ColoredBitmap.CreateBitmap(100, 100);
            var          usage = new ChannelUsage(2);

            var pic     = Interface.Encrypt(image, usage, 0, text);
            var resText = Interface.Decrypt(pic, usage, 0);

            Assert.AreEqual(text, resText);
        }
Пример #9
0
        public void TestProcessLarge()
        {
            const string text  = "Pretty long text just to test if everything is working properly! I'll therefore add some more text so the whole process takes some more time which makes it more suitable to compare the performance as well as changes to it.";
            var          image = ColoredBitmap.CreateBitmap(1920, 1080);
            var          usage = new ChannelUsage(2);

            var pic     = Interface.Encrypt(image, usage, 0, text);
            var resText = Interface.Decrypt(pic, usage, 0);

            Assert.AreEqual(text, resText);
        }
Пример #10
0
        public void TestEncrypt()
        {
            const string text    = "Sample text to hide";
            var          image   = ColoredBitmap.CreateBitmap(100, 100);
            var          usage   = new ChannelUsage(2);
            var          picture = Interface.Encrypt(image, usage, 0, text);

            var decrypter = new Decrypter(new Picture(picture));
            var resText   = decrypter.ReadText(usage);

            Assert.AreEqual(text, resText);
        }
Пример #11
0
        public void TestNoAlpha()
        {
            const string text      = "Sample Text written with 2 bits per channel, but not using the alpha channel";
            var          usage     = new ChannelUsage(2, 2, 2);
            var          encrypter = new Encrypter(new Picture(new Bitmap(20, 20)));

            encrypter.InsertText(text, usage);
            var picture = encrypter.GetPictureData();

            var decrypter = new Decrypter(picture);
            var res       = decrypter.ReadText(usage);

            Assert.AreEqual(text, res);
        }
Пример #12
0
        public void TestChnUsage()
        {
            const string text      = "Sample Text written with 3 bits per channel which gives the possibility to store more text";
            var          usage     = new ChannelUsage(3);
            var          encrypter = new Encrypter(new Picture(new Bitmap(20, 20)));

            encrypter.InsertText(text, usage);
            var picture = encrypter.GetPictureData();

            var decrypter = new Decrypter(picture);
            var res       = decrypter.ReadText(usage);

            Assert.AreEqual(text, res);
        }
Пример #13
0
        public void TestLong()
        {
            const string text      = "Pretty long text just to test if everything is working properly!";
            var          usage     = new ChannelUsage(2);
            var          encrypter = new Encrypter(new Picture(ColoredBitmap.CreateBitmap(20, 20, Color.Aqua), 0));

            encrypter.InsertText(text, usage);
            var picture = encrypter.GetPictureData();

            var decrypter = new Decrypter(picture);
            var res       = decrypter.ReadText(usage);

            Assert.AreEqual(text, res);
        }
Пример #14
0
        public void TestShort()
        {
            const string text      = "Sample Text to hide";
            var          usage     = new ChannelUsage(2);
            var          encrypter = new Encrypter(new Picture(ColoredBitmap.CreateBitmap(10, 10, Color.Aqua), 0));

            encrypter.InsertText(text, usage);
            var picture = encrypter.GetPictureData();

            var decrypter = new Decrypter(picture);
            var res       = decrypter.ReadText(usage);

            Assert.AreEqual(text, res);
        }
Пример #15
0
        public void TestDifferentChnUsage()
        {
            const string text      = "Sample Text written with different bits used per channel, but not using the alpha channel";
            var          usage     = new ChannelUsage(2, 3, 0, 1);
            var          encrypter = new Encrypter(new Picture(100, 100));

            encrypter.InsertText(text, usage);
            var picture = encrypter.GetPictureData();

            var decrypter = new Decrypter(picture);
            var res       = decrypter.ReadText(usage);

            Assert.AreEqual(text, res);
        }
Пример #16
0
        public void TestSeed()
        {
            const int    seed      = 23;
            const string text      = "Sample Text to hide";
            var          usage     = new ChannelUsage(2);
            var          encrypter = new Encrypter(new Picture(new Bitmap(20, 20), seed));

            encrypter.InsertText(text, usage);
            var picture = encrypter.GetPictureData();

            var decrypter = new Decrypter(picture);
            var res       = decrypter.ReadText(usage);

            Assert.AreEqual(text, res);
        }
Пример #17
0
        public void TestWrongSeed()
        {
            const int    seed      = 26;
            const string text      = "This won't work because we use another seed for decrypting our image";
            var          usage     = new ChannelUsage(2);
            var          encrypter = new Encrypter(new Picture(new Bitmap(20, 20), seed));

            encrypter.InsertText(text, usage);
            var picture = encrypter.GetPictureData();

            var decrypter = new Decrypter(new Picture(picture.GetImage(), 0));
            var res       = decrypter.ReadText(usage);

            Assert.AreNotEqual(text, res);
        }
Пример #18
0
        public void TestWrongChnUsage()
        {
            const string text      = "Sample Text written with 2 bits per channel, but not using the alpha channel while encrypting but using it when decrypting";
            var          usage     = new ChannelUsage(2, 3, 3, 2);
            var          encrypter = new Encrypter(new Picture(100, 100));

            encrypter.InsertText(text, usage);
            var picture = encrypter.GetPictureData();

            var decrypter = new Decrypter(picture);

            usage = new ChannelUsage(3, 2, 2, 3);
            var res = decrypter.ReadText(usage);

            Assert.AreNotEqual(text, res);
        }
Пример #19
0
        public void TestDecrypt()
        {
            const string text    = "Sample text to hide";
            var          image   = ColoredBitmap.CreateBitmap(100, 100);
            var          usage   = new ChannelUsage(2);
            var          picture = new Encrypter(new Picture(image));

            picture.InsertText(text, usage);

            var endImage = picture.GetPictureData().GetImage();

            endImage.Save("image.png", ImageFormat.Png);

            var resText = Interface.Decrypt(endImage, usage, 0);

            Assert.AreEqual(text, resText);
        }
Пример #20
0
    private float GetColor(ChannelUsage usage, Color c)
    {
        switch (usage)
        {
        case ChannelUsage.A:
            return(c.a);

        case ChannelUsage.R:
            return(c.r);

        case ChannelUsage.B:
            return(c.b);

        case ChannelUsage.G:
            return(c.g);
        }
        return(0);
    }
Пример #21
0
        public void TestSingleCharTagless()
        {
            const string text  = "s";
            var          image = ColoredBitmap.CreateBitmap(1, 1);
            var          usage = new ChannelUsage(2);

            var encrypter = new Encrypter(new Picture(image));

            encrypter.InsertText(text, usage, false);

            var resImage = encrypter.GetPictureData().GetImage();

            encrypter.Dispose();

            var decrypter = new Decrypter(new Picture(resImage));
            var resText   = decrypter.ReadText(usage, false);

            Assert.AreEqual(text, resText);
        }
Пример #22
0
        internal void InsertText(string text, ChannelUsage usage, bool useTags = true)
        {
            if (useTags)
            {
                text = "<start>" + text + "<end>";
            }

            //counters
            var pixelIndex    = 0;
            var channelIndex  = 0;
            var positionIndex = 0;

            //other info
            var currentChannelUsage = usage.GetChannelUsage(0);

            var bytes = Encoding.UTF8.GetBytes(text.ToCharArray());

            for (var bitIndex = 0; bitIndex < bytes.Length * 8; bitIndex++, positionIndex++)
            {
                //get correct position
                while (positionIndex >= currentChannelUsage)
                {
                    channelIndex++;
                    positionIndex = 0;
                    if (channelIndex >= 4)
                    {
                        pixelIndex++;
                        channelIndex = 0;
                    }
                    currentChannelUsage = usage.GetChannelUsage((Channel)channelIndex);
                }

                //insert
                int data = _workingData.GetPixelChannel(pixelIndex, (Channel)channelIndex);

                var valByte = bytes[bitIndex >> 3];
                var val     = BitOperators.ReadOutByte(valByte, 7 ^ (bitIndex & 7), 1, true) == 1;

                var result = BitOperators.InsertBit((byte)data, positionIndex, val);

                _workingData.SetPixelChannel(pixelIndex, (Channel)channelIndex, result);
            }
        }
Пример #23
0
    private void OnGUI()
    {
        string     path;
        Vector2Int size;

        if (r)
        {
            path = AssetDatabase.GetAssetPath(r) + "_" + fileName + ".png";
            size = new Vector2Int(r.width, r.height);
        }
        else if (g)
        {
            path = AssetDatabase.GetAssetPath(g) + "_" + fileName + ".png";
            size = new Vector2Int(g.width, g.height);
        }
        else if (b)
        {
            path = AssetDatabase.GetAssetPath(b) + "_" + fileName + ".png";
            size = new Vector2Int(b.width, b.height);
        }
        else if (a)
        {
            path = AssetDatabase.GetAssetPath(a) + "_" + fileName + ".png";
            size = new Vector2Int(a.width, a.height);
        }
        else
        {
            path = "Assets/" + fileName + ".png";
            size = new Vector2Int(1, 1);
        }
        r = EditorGUILayout.ObjectField("R Channel Texture: ", r, typeof(Texture2D), false) as Texture2D;
        rTextureChannel = (ChannelUsage)EditorGUILayout.EnumPopup("R Texture Channel: ", rTextureChannel);
        g = EditorGUILayout.ObjectField("G Channel Texture: ", g, typeof(Texture2D), false) as Texture2D;
        gTextureChannel = (ChannelUsage)EditorGUILayout.EnumPopup("G Texture Channel: ", gTextureChannel);
        b = EditorGUILayout.ObjectField("B Channel Texture: ", b, typeof(Texture2D), false) as Texture2D;
        bTextureChannel = (ChannelUsage)EditorGUILayout.EnumPopup("B Texture Channel: ", bTextureChannel);
        a = EditorGUILayout.ObjectField("A Channel Texture: ", a, typeof(Texture2D), false) as Texture2D;
        aTextureChannel = (ChannelUsage)EditorGUILayout.EnumPopup("A Texture Channel: ", aTextureChannel);
        reverseR        = EditorGUILayout.Toggle("Reverse R Channel", reverseR);
        srgb            = EditorGUILayout.Toggle("SRGB", srgb);
        if (GUILayout.Button("Combine Texture"))
        {
            if (r && !r.isReadable)
            {
                Debug.LogError("You have to set R channel Texture as readable!");
                return;
            }
            if (g && !g.isReadable)
            {
                Debug.LogError("You have to set G channel Texture as readable!");
                return;
            }
            if (b && !b.isReadable)
            {
                Debug.LogError("You have to set B channel Texture as readable!");
                return;
            }
            if (a && !a.isReadable)
            {
                Debug.LogError("You have to set A channel Texture as readable!");
                return;
            }
            Texture2D resultTex = new Texture2D(size.x, size.y, TextureFormat.ARGB32, false, !srgb);
            Color[]   colors    = new Color[size.x * size.y];
            for (int x = 0; x < size.x; ++x)
            {
                for (int y = 0; y < size.y; ++y)
                {
                    Color cl = new Color
                    {
                        r = r ? GetColor(rTextureChannel, r.GetPixelBilinear((x + 0.5f) / size.x, (y + 0.5f) / size.y)) : 1,
                        g = g ? GetColor(gTextureChannel, g.GetPixelBilinear((x + 0.5f) / size.x, (y + 0.5f) / size.y)) : 1,
                        b = b ? GetColor(bTextureChannel, b.GetPixelBilinear((x + 0.5f) / size.x, (y + 0.5f) / size.y)) : 1,
                        a = a ? GetColor(aTextureChannel, a.GetPixelBilinear((x + 0.5f) / size.x, (y + 0.5f) / size.y)) : 1
                    };
                    colors[y * size.x + x] = cl;
                }
            }
            if (reverseR)
            {
                for (int i = 0; i < colors.Length; ++i)
                {
                    colors[i].r = 1 - colors[i].r;
                }
            }
            resultTex.SetPixels(colors);
            resultTex.Apply();

            System.IO.File.WriteAllBytes(path, resultTex.EncodeToPNG());
        }
    }
Пример #24
0
 /// <summary>
 /// Encrypts a text into a BitmapImage
 /// </summary>
 /// <param name="picture">The picture the text will be encrypted into</param>
 /// <param name="usage">How many bits will be used in each channel</param>
 /// <param name="seed">The seed used in the generator for the order of pixels</param>
 /// <param name="text">Text that should be inserted</param>
 /// <returns>BitmapImage with slightly changed pixel colors</returns>
 public static BitmapImage Encrypt(BitmapImage picture, ChannelUsage usage, int seed, string text)
 {
     return((BitmapImage)Encrypt(picture.ToWinFormsBitmap(), usage, seed, text).ToWpfBitmap());
 }