Example #1
0
        public void EncodeImage_Theory(string fileName)
        {
            string file    = Path.Combine(Util.ArtifactsPath, fileName);
            string outFile = Path.Combine(Util.ArtifactsDataPath, fileName + ".djvu");

            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(file))
            {
                bmp.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
                int width  = bmp.Width;
                int height = bmp.Height;
                var pixMap = PixelMapFromBitmap(bmp);

                var map = new InterWavePixelMapEncoder();
                map.InitializeEncoder(pixMap, null, YCrCbMode.Full);

                int   nchunks = 4;
                int[] slices  = new int[] { 74, 90, 98, 103 };
                //float[] decibel = new float[] { 5.0f, 10.0f, 15.0f, 20.0f };

                InterWaveEncoderSettings[] settings = new InterWaveEncoderSettings[nchunks];
                if (fileName != "")
                {
                    for (int i = 0; i < nchunks; i++)
                    {
                        settings[i] = new InterWaveEncoderSettings
                        {
                            Slices = slices[i]
                        };
                    }
                }
                //else
                //{
                //    for (int i = 0; i < nchunks; i++)
                //    {
                //        settings[i] = new InterWaveEncoderSettings
                //        {
                //            Decibels = decibel[i]
                //        };
                //    }
                //}

                DjvuFormElement form = null;
                using (MemoryStream stream = new MemoryStream())
                    using (IDjvuWriter writer = new DjvuWriter(stream))
                        form = map.EncodeImage(writer, nchunks, settings);

                using (IDjvuWriter writer = new DjvuWriter(outFile))
                    form.WriteData(writer);

                using (DjvuDocument doc = new DjvuDocument(outFile))
                {
                    IDjvuPage page     = doc.Pages[0];
                    PM44Form  pageForm = (PM44Form)page.PageForm;
                    Assert.Equal(nchunks, pageForm.Children.Count);

                    Assert.NotNull(form);
                    Assert.Equal(nchunks, form.Children.Count);
                    Assert.IsType <PM44Form>(form);
                    for (int i = 0; i < form.Children.Count; i++)
                    {
                        var c = form.Children[i];
                        Assert.NotNull(c);
                        Assert.IsType <PM44Chunk>(c);
                        PM44Chunk chunk = (PM44Chunk)c;
                        Assert.NotNull(chunk.ChunkData);
                        if (chunk.ChunkData.Length >= 2)
                        {
                            Assert.Equal(i, chunk.ChunkData[0]);
                            Assert.Equal(slices[i] - (i == 0 ? 0 : slices[i - 1]), chunk.ChunkData[1]);
                            if (i == 0)
                            {
                                Assert.Equal(1, chunk.ChunkData[2]);
                                Assert.Equal(2, chunk.ChunkData[3]);

                                int widthTest = chunk.ChunkData[4] << 8;
                                widthTest |= chunk.ChunkData[5];
                                Assert.Equal(bmp.Width, widthTest);

                                int heightTest = chunk.ChunkData[6] << 8;
                                heightTest |= chunk.ChunkData[7];
                                Assert.Equal(bmp.Height, heightTest);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        public void EncodeImage_Theory(string fileName)
        {
            string file    = Path.Combine(Util.ArtifactsPath, fileName);
            string outFile = Path.Combine(Util.ArtifactsDataPath, fileName + ".djvu");

            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(file))
            {
                bmp.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
                int       width  = bmp.Width;
                int       height = bmp.Height;
                IPixelMap pixMap = PixelMapFromBitmap(bmp);

                var map = new InterWavePixelMapEncoder();
                map.InitializeEncoder(pixMap, null, YCrCbMode.Full);

                const int nchunks = 4;
                int[]     slices  = new int[] { 74, 90, 98, 103 };
                //float[] decibel = new float[] { 5.0f, 10.0f, 15.0f, 20.0f };

                InterWaveEncoderSettings[] settings = new InterWaveEncoderSettings[nchunks];
                if (fileName != "")
                {
                    for (int i = 0; i < nchunks; i++)
                    {
                        settings[i] = new InterWaveEncoderSettings {
                            Slices = slices[i]
                        };
                    }
                }
                //else
                //{
                //    for (int i = 0; i < nchunks; i++)
                //    {
                //        settings[i] = new InterWaveEncoderSettings
                //        {
                //            Decibels = decibel[i]
                //        };
                //    }
                //}

                DjvuFormElement form = null;
                using (MemoryStream stream = new MemoryStream())
                    using (IDjvuWriter writer = new DjvuWriter(stream))
                    {
                        form = map.EncodeImage(writer, nchunks, settings);
                    }

                using (IDjvuWriter writer = new DjvuWriter(outFile))
                {
                    form.WriteData(writer);
                }

                using (DjvuDocument doc = new DjvuDocument(outFile))
                {
                    IDjvuPage page     = doc.Pages[0];
                    PM44Form  pageForm = (PM44Form)page.PageForm;

                    if (nchunks != pageForm.Children.Count)
                    {
                        Assert.True(false);
                    }

                    if (nchunks != form.Children.Count)
                    {
                        Assert.True(false);
                    }

                    Assert.IsType <PM44Form>(form);

                    for (int i = 0; i < nchunks; i++)
                    {
                        IDjvuNode c = form.Children[i];

                        if (c == null)
                        {
                            Assert.True(false);
                        }

                        byte[] chunkDataBuffer = null;
                        if (!(c is PM44Chunk chunk))
                        {
                            Assert.True(false);
                        }
Example #3
0
        public void EncodeImageTest003()
        {
            string file    = Path.Combine(Util.ArtifactsPath, "block001C.png");
            string outFile = Path.Combine(Util.ArtifactsDataPath, "block001C.png.djvu");

            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(file))
            {
                bmp.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
                int width  = bmp.Width;
                int height = bmp.Height;
                var pixMap = PixelMapFromBitmap(bmp);

                var map = new InterWavePixelMapEncoder();
                map._CrCbDelay = 0;
                map.InitializeEncoder(pixMap, null, YCrCbMode.Full);

                int   nchunks = 4;
                int[] slices  = new int[] { 111, 152, 184, 200 };

                InterWaveEncoderSettings[] settings = new InterWaveEncoderSettings[nchunks];
                for (int i = 0; i < nchunks; i++)
                {
                    settings[i] = new InterWaveEncoderSettings
                    {
                        Slices = slices[i]
                    };
                }

                map._dBFrac = 10.0f;

                DjvuFormElement form = null;
                using (MemoryStream stream = new MemoryStream())
                    using (IDjvuWriter writer = new DjvuWriter(stream))
                        form = map.EncodeImage(writer, nchunks, settings);

                using (IDjvuWriter writer = new DjvuWriter(outFile))
                    form.WriteData(writer);

                Assert.NotNull(form);
                Assert.Equal(nchunks, form.Children.Count);
                Assert.IsType <PM44Form>(form);
                for (int i = 0; i < form.Children.Count; i++)
                {
                    var c = form.Children[i];
                    Assert.NotNull(c);
                    Assert.IsType <PM44Chunk>(c);
                    PM44Chunk chunk = (PM44Chunk)c;
                    Assert.NotNull(chunk.ChunkData);
                    if (chunk.ChunkData.Length >= 2)
                    {
                        Assert.Equal(i, chunk.ChunkData[0]);
                        if (i == 0)
                        {
                            Assert.Equal(1, chunk.ChunkData[2]);
                            Assert.Equal(2, chunk.ChunkData[3]);

                            int widthTest = chunk.ChunkData[4] << 8;
                            widthTest |= chunk.ChunkData[5];
                            Assert.Equal(bmp.Width, widthTest);

                            int heightTest = chunk.ChunkData[6] << 8;
                            heightTest |= chunk.ChunkData[7];
                            Assert.Equal(bmp.Height, heightTest);
                        }
                    }
                }



                //using (DjvuDocument doc = new DjvuDocument(outFile))
                //{
                //    IDjvuPage page = doc.Pages[0];
                //    IDjvuElement pageForm = page.PageForm;
                //    Assert.Equal(nchunks, pageForm.Children.Count);
                //}
            }
        }