Exemplo n.º 1
0
 public DCMatrix(string path)
 {
     BitMiracle.LibJpeg.Classic.jpeg_decompress_struct oJpegDecompress = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
     System.IO.FileStream oFileStreamImage;
     try
     {
         oFileStreamImage = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
     }
     catch
     {
         throw new Exception("Error reading");
     }
     oJpegDecompress.jpeg_stdio_src(oFileStreamImage);
     oJpegDecompress.jpeg_read_header(true);
     BitMiracle.LibJpeg.Classic.jvirt_array <BitMiracle.LibJpeg.Classic.JBLOCK>[] JBlock = oJpegDecompress.jpeg_read_coefficients();
     if (JBlock[2] != null)
     {
         ExtractChannel(oJpegDecompress, JBlock, 0, ref Y);
         ExtractChannel(oJpegDecompress, JBlock, 1, ref Cb);
         ExtractChannel(oJpegDecompress, JBlock, 2, ref Cr);
     }
     else
     {
         ExtractChannel(oJpegDecompress, JBlock, 0, ref B);
     }
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //Console.WriteLine("args[1]:%s", args[1]);
            string sourceImagePath      = null;
            string destinationImagePath = null;

            //引数の数がおかしい時の処理h
            //今は雑デバッグ向けに暫定対応
            if (args.Length != 2)
            {
                Console.Write("入力ファイルパスを入力してね");
                sourceImagePath = Console.ReadLine();
                Console.Write("出力ファイルパスを入力してね");
                destinationImagePath = Console.ReadLine();
            }

            /*else
             * {
             *  sourceImagePath = args[1];
             *  destinationImagePath = args[2];
             * }*/

            //元ファイルを開いてDCT係数を引っこ抜く
            try
            {
                using (System.IO.FileStream sourceImageStream = new System.IO.FileStream(sourceImagePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                    using (System.IO.FileStream destinationImageStream = new System.IO.FileStream(destinationImagePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
                    {
                        //todo: ここにJPEGのDCT係数を引っこ抜く処理を書く
                        BitMiracle.LibJpeg.Classic.jpeg_decompress_struct sourceImageStruct = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
                        sourceImageStruct.jpeg_stdio_src(sourceImageStream);
                        sourceImageStruct.jpeg_read_header(true);
                    }
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("何かおかしいです");
                throw;
            }
            catch (System.Security.SecurityException ex)
            {
                Console.WriteLine("ファイルへのアクセス権がありません");
                throw;
            }
            catch (System.IO.IOException ex)
            {
                Console.WriteLine("ファイルにアクセスしようとしたらエラーを吐きました");
                throw;
            }
        }
Exemplo n.º 3
0
        private void ExtractChannel(BitMiracle.LibJpeg.Classic.jpeg_decompress_struct oJpegDecompress,
                                    BitMiracle.LibJpeg.Classic.jvirt_array <BitMiracle.LibJpeg.Classic.JBLOCK>[] JBlock,
                                    int c, ref short[][] Target)
        {
            var lengthOfChannel = oJpegDecompress.Comp_info[c].Width_in_blocks;
            var reader          = JBlock[c].Access(0, 1);
            int realWidth       = 0;

            for (int i = 0; i < lengthOfChannel; i++)
            {
                try
                {
                    reader = JBlock[c].Access(i, 1);
                }
                catch
                {
                    realWidth = i;
                    break;
                }
            }
            realWidth   = (realWidth != 0) ? realWidth : lengthOfChannel;
            reader      = JBlock[c].Access(0, realWidth);
            numOfMatrix = 0;
            int sizeX = reader.Length;

            Target = new short[reader.Length * reader[0].Length][];
            for (int i = 0; i < reader.Length; i++)
            {
                for (int j = 0; j < reader[i].Length; j++)
                {
                    Target[numOfMatrix] = new short[64];
                    for (int k = 0; k < 64; k++)
                    {
                        Target[numOfMatrix][k] = reader[i][j][k];
                    }
                    ++numOfMatrix;
                }
            }
        }
Exemplo n.º 4
0
        private async void bEmbed(object sender, RoutedEventArgs e)
        {
            txtData = new TextRange(etxtMessage.Document.ContentStart, etxtMessage.Document.ContentEnd).Text;

            if (etxtImagePath.Text.Length == 0 && txtData.Length == 0)
            {
                await this.ShowMessageAsync("Error:", "Image file and text data is not selected.");

                return;
            }
            else if (etxtImagePath.Text.Length == 0)
            {
                await this.ShowMessageAsync("Error:", "Image file and is not selected.");

                return;
            }
            else if (txtData.Length == 0)
            {
                await this.ShowMessageAsync("Error:", "Enter text to hide.");

                return;
            }
            try
            {
                var controller = await this.ShowProgressAsync("Please wait:", "Embedding...");

                BitMiracle.LibJpeg.Classic.jpeg_decompress_struct oJpegDecompress = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
                System.IO.FileStream oFileStreamImage = new System.IO.FileStream(etxtImagePath.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                oJpegDecompress.jpeg_stdio_src(oFileStreamImage);
                oJpegDecompress.jpeg_read_header(true);
                BitMiracle.LibJpeg.Classic.jvirt_array <BitMiracle.LibJpeg.Classic.JBLOCK>[] DCTcoff = oJpegDecompress.jpeg_read_coefficients();

                int hightTemp          = (int)Math.Ceiling(coverimage.Height / 8.0);
                int widthTemp          = (int)Math.Ceiling(coverimage.Width / 8.0);
                int widthComponent1    = widthTemp % 2 == 0 ? widthTemp : widthTemp + 1;
                int heightComponent1   = hightTemp % 2 == 0 ? hightTemp : hightTemp + 1;
                int widthComponent2_3  = widthComponent1 / 2;
                int heightComponent2_3 = heightComponent1 / 2;

                string dataBitsToSave      = string.Join("", Encoding.UTF8.GetBytes(txtData).Select(b => Convert.ToString(b, 2).PadLeft(8, '0'))); dataBitsToSave += "00000000";
                Int32  number_SavedBits    = 0;
                bool   operationTerminator = false;


                for (int colorIterate = 0; colorIterate < 3 && !operationTerminator; colorIterate++)
                {
                    int height = colorIterate == 0 ? heightComponent1 : heightComponent2_3, width = colorIterate == 0 ? widthComponent1 : widthComponent2_3;
                    for (int heightIteratein2D = 0; heightIteratein2D < height && !operationTerminator; heightIteratein2D++)
                    {
                        for (int widthIteratein2D = 0; widthIteratein2D < width && !operationTerminator; widthIteratein2D++)
                        {
                            for (int cofficentNumber = 1; cofficentNumber < 64 && !operationTerminator; cofficentNumber++)
                            {
                                short cofficent = DCTcoff[colorIterate].Access(heightIteratein2D, 1)[0][widthIteratein2D][cofficentNumber];
                                if (number_SavedBits == dataBitsToSave.Length)
                                {
                                    operationTerminator = true;
                                }
                                else
                                {
                                    if (!dataBitsToSave[number_SavedBits].ToString().Equals((cofficent & 1).ToString()))
                                    {
                                        DCTcoff[colorIterate].Access(heightIteratein2D, 1)[0][widthIteratein2D][cofficentNumber] = (short)(cofficent ^ 1);
                                    }
                                    number_SavedBits++;
                                }
                            }
                        }
                    }
                }


                oJpegDecompress.jpeg_finish_decompress();
                oFileStreamImage.Close();

                System.IO.FileStream objFileStreamMegaMap = System.IO.File.Create(@"" + etxtImagePath.Text.Substring(0, etxtImagePath.Text.LastIndexOf(".")) + "_steg.JPG");
                BitMiracle.LibJpeg.Classic.jpeg_compress_struct oJpegCompress = new BitMiracle.LibJpeg.Classic.jpeg_compress_struct();
                oJpegCompress.jpeg_stdio_dest(objFileStreamMegaMap);
                oJpegDecompress.jpeg_copy_critical_parameters(oJpegCompress);
                oJpegCompress.Image_height = coverimage.Height;
                oJpegCompress.Image_width  = coverimage.Width;
                oJpegCompress.jpeg_write_coefficients(DCTcoff);
                oJpegCompress.jpeg_finish_compress();
                objFileStreamMegaMap.Close();
                oJpegDecompress.jpeg_abort_decompress();
                oFileStreamImage.Close();
                await controller.CloseAsync();

                await this.ShowMessageAsync("Embedding Successful:", "File is saved to orignal image file directory with _steg after the original name.");
            }
            catch (Exception excep)
            { await this.ShowMessageAsync("Exception:", excep.Message); }
        }
Exemplo n.º 5
0
        private async void messageExtract(object sender, RoutedEventArgs e)
        {
            if (dtxtImagePath.Text.Length == 0)
            {
                await this.ShowMessageAsync("Error:", "Image file is not selected.");

                return;
            }
            try
            {
                var controller = await this.ShowProgressAsync("Please wait:", "Extracting...");

                BitMiracle.LibJpeg.Classic.jpeg_decompress_struct oJpegDecompress = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
                System.IO.FileStream oFileStreamImage = new System.IO.FileStream(dtxtImagePath.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                oJpegDecompress.jpeg_stdio_src(oFileStreamImage);
                oJpegDecompress.jpeg_read_header(true);
                BitMiracle.LibJpeg.Classic.jvirt_array <BitMiracle.LibJpeg.Classic.JBLOCK>[] DCTcoff = oJpegDecompress.jpeg_read_coefficients();

                int hightTemp          = (int)Math.Ceiling(coverimage.Height / 8.0);
                int widthTemp          = (int)Math.Ceiling(coverimage.Width / 8.0);
                int widthComponent1    = widthTemp % 2 == 0 ? widthTemp : widthTemp + 1;
                int heightComponent1   = hightTemp % 2 == 0 ? hightTemp : hightTemp + 1;
                int widthComponent2_3  = widthComponent1 / 2;
                int heightComponent2_3 = heightComponent1 / 2;

                StringBuilder dataBitsRetreived   = new StringBuilder();
                int           number_RetrivedBits = 0;
                bool          operationTerminator = false;


                for (int colorIterate = 0; colorIterate < 3 && !operationTerminator; colorIterate++)
                {
                    int height = colorIterate == 0 ? heightComponent1 : heightComponent2_3, width = colorIterate == 0 ? widthComponent1 : widthComponent2_3;
                    for (int heightIteratein2D = 0; heightIteratein2D < height && !operationTerminator; heightIteratein2D++)
                    {
                        for (int widthIteratein2D = 0; widthIteratein2D < width && !operationTerminator; widthIteratein2D++)
                        {
                            for (int l = 1; l < 64 && !operationTerminator; l++)
                            {
                                short cofficent = DCTcoff[colorIterate].Access(heightIteratein2D, 1)[0][widthIteratein2D][l];
                                dataBitsRetreived.Append(cofficent & 1);
                                if (dataBitsRetreived.Length % 8 == 0 && dataBitsRetreived.ToString(dataBitsRetreived.Length - 8, 8).Equals("00000000"))
                                {
                                    operationTerminator = true;
                                }
                                else
                                {
                                    number_RetrivedBits++;
                                }
                            }
                        }
                    }
                }
                oJpegDecompress.jpeg_finish_decompress();
                oFileStreamImage.Close();

                string dataBitsToWrite = dataBitsRetreived.ToString(0, dataBitsRetreived.Length - 8);
                byte[] dataBytes       = new byte[dataBitsToWrite.Length / 8];
                for (int i = 0; i + 8 <= dataBitsToWrite.Length; i = i + 8)
                {
                    dataBytes[i / 8] = Convert.ToByte(dataBitsToWrite.Substring(i, 8), 2);
                }
                output.Text = Encoding.UTF8.GetString(dataBytes);
                output.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                await controller.CloseAsync();

                await this.ShowMessageAsync("Extraction Successful:", "Text is displayed in the text box.");
            }
            catch (Exception excep)
            { await this.ShowMessageAsync("Exception:", excep.Message); }
        }