Пример #1
0
        public void WriteDataTest002()
        {
            Mock <DjvuNode> nodeMock = new Mock <DjvuNode>()
            {
                CallBase = true
            };
            Mock <IDjvuReader> readerMock = new Mock <IDjvuReader>();

            using (MemoryStream stream = new MemoryStream(4096))
                using (DjvuWriter writer = new DjvuWriter(stream))
                {
                    string chunkID = "FORM";

                    nodeMock.Object.DataOffset = 1024;
                    nodeMock.Object.Length     = 2048;
                    nodeMock.Object.Reader     = readerMock.Object;
                    nodeMock.Setup <ChunkType>(x => x.ChunkType).Returns(ChunkType.Form);
                    nodeMock.Object.ChunkID = chunkID;

                    byte[] buffer = new byte[nodeMock.Object.Length];
                    nodeMock.Setup <byte[]>(x => x.ChunkData).Returns(buffer);

                    Assert.Throws <DjvuArgumentNullException>("writer", () => nodeMock.Object.WriteData(null));
                }
        }
Пример #2
0
        public void DjvuWriterTest1()
        {
            string     filePath = Path.GetTempFileName();
            DjvuWriter writer   = null;

            try
            {
                using (writer = new DjvuWriter(filePath))
                {
                    Assert.NotNull(writer);
                    Assert.IsType <DjvuWriter>(writer);
                    var ibw = writer as IBinaryWriter;
                    Assert.NotNull(ibw);
                    var idw = ibw as IDjvuWriter;
                    Assert.NotNull(idw);
                    Assert.NotNull(writer.BaseStream);
                    Assert.IsType <FileStream>(writer.BaseStream);
                }
            }
            finally
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
        }
Пример #3
0
        public void WriteJPEGImageTest()
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    Assert.Throws <NotImplementedException>(() => writer.WriteJPEGImage(null));
                }
        }
Пример #4
0
        public void CloneWriterTest1()
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    Assert.Throws <NotImplementedException>(() => writer.CloneWriter(4096, 4096));
                }
        }
Пример #5
0
        public void WriteStringStringEncodingTest002()
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    string   testString = "";
                    Encoding encoding   = null;
                    Assert.Throws <ArgumentNullException>("encoding", () => writer.WriteString(testString, encoding));
                }
        }
Пример #6
0
        public void WriteDataTest001()
        {
            Mock <DjvuNode> nodeMock = new Mock <DjvuNode>()
            {
                CallBase = true
            };
            Mock <IDjvuReader> readerMock = new Mock <IDjvuReader>();

            using (MemoryStream stream = new MemoryStream(4096))
                using (DjvuWriter writer = new DjvuWriter(stream))
                {
                    string chunkID          = "FORM";
                    string startOfChunkData = "Start of ChunkData";
                    string endOfChunkData   = "End of ChunkData";

                    nodeMock.Object.DataOffset = 1024;
                    nodeMock.Object.Length     = 2048;
                    nodeMock.Object.Reader     = readerMock.Object;
                    nodeMock.Setup <ChunkType>(x => x.ChunkType).Returns(ChunkType.Form);
                    nodeMock.Object.ChunkID = chunkID;

                    byte[] buffer    = new byte[nodeMock.Object.Length];
                    byte[] startData = Encoding.ASCII.GetBytes(startOfChunkData);
                    Buffer.BlockCopy(startData, 0, buffer, 0, startData.Length);
                    byte[] endOfData = Encoding.ASCII.GetBytes(endOfChunkData);
                    Buffer.BlockCopy(endOfData, 0, buffer, buffer.Length - endOfData.Length, endOfData.Length);

                    nodeMock.Setup <byte[]>(x => x.ChunkData).Returns(buffer);

                    nodeMock.Object.WriteData(writer);

                    byte[] testBuffer = stream.GetBuffer();

                    string nodeName = Encoding.UTF8.GetString(testBuffer, 0, 4);
                    Assert.False(String.IsNullOrWhiteSpace(nodeName));
                    Assert.Equal(chunkID, nodeName);

                    byte[] lengthBytes = new byte[4];
                    Buffer.BlockCopy(testBuffer, 4, lengthBytes, 0, 4);
                    Array.Reverse(lengthBytes);
                    uint dataLength = BitConverter.ToUInt32(lengthBytes, 0);
                    Assert.Equal <uint>((uint)nodeMock.Object.Length, dataLength);

                    string startOfData = Encoding.ASCII.GetString(testBuffer, 8, startData.Length);
                    Assert.False(String.IsNullOrWhiteSpace(startOfData));
                    Assert.Equal(startOfChunkData, startOfData);

                    string endData = Encoding.ASCII.GetString(testBuffer, 8 + (int)nodeMock.Object.Length - endOfData.Length, endOfData.Length);
                    Assert.False(String.IsNullOrWhiteSpace(endData));
                    Assert.Equal(endOfChunkData, endData);
                }
        }
Пример #7
0
        public void GetBZZEncodedWriterTest1()
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    BzzWriter bzzWriter = null;
                    bzzWriter = writer.GetBZZEncodedWriter();
                    Assert.NotNull(bzzWriter);
                    Assert.IsType <BzzWriter>(bzzWriter);
                }
        }
Пример #8
0
        public void WriteUInt24_Theory(uint test)
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    writer.WriteUInt24(test);
                    byte[] buffer     = stream.GetBuffer();
                    byte[] testBuffer = new byte[4];
                    Buffer.BlockCopy(buffer, 0, testBuffer, 0, 3);
                    Assert.Equal <uint>(test, BitConverter.ToUInt32(testBuffer, 0));
                }
        }
Пример #9
0
        public void PositionTest()
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    Assert.Equal(0, writer.Position);
                    Assert.Equal(stream.Position, writer.Position);

                    writer.Position = 0x2048;
                    Assert.Equal(0x2048, writer.Position);
                    Assert.Equal(stream.Position, writer.Position);
                }
        }
Пример #10
0
        public void WriteUInt64BigEndian_Theory(ulong test)
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    writer.WriteUInt64BigEndian(test);
                    byte[] buffer     = stream.GetBuffer();
                    byte[] testBuffer = new byte[8];
                    Buffer.BlockCopy(buffer, 0, testBuffer, 0, 8);
                    Array.Reverse(testBuffer);
                    Assert.Equal <ulong>(test, BitConverter.ToUInt64(testBuffer, 0));
                }
        }
Пример #11
0
        public void WriteInt32BigEndian_Theory(int test)
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    writer.WriteInt32BigEndian(test);
                    byte[] buffer     = stream.GetBuffer();
                    byte[] testBuffer = new byte[4];
                    Buffer.BlockCopy(buffer, 0, testBuffer, 0, 4);
                    Array.Reverse(testBuffer);
                    Assert.Equal <int>(test, BitConverter.ToInt32(testBuffer, 0));
                }
        }
Пример #12
0
        public void ToStringTest()
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    string test = null;
                    test = writer.ToString();
                    Assert.False(String.IsNullOrWhiteSpace(test));
                    Assert.Contains(nameof(DjvuWriter), test);
                    Assert.Contains("Position: ", test);
                    Assert.Contains("Length: ", test);
                    Assert.Contains("BaseStream: ", test);
                    Assert.Contains(nameof(MemoryStream), test);
                }
        }
Пример #13
0
        public void DjvuWriterTest()
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    Assert.NotNull(writer);
                    Assert.IsType <DjvuWriter>(writer);
                    var ibw = writer as IBinaryWriter;
                    Assert.NotNull(ibw);
                    var idw = ibw as IDjvuWriter;
                    Assert.NotNull(idw);
                    Assert.NotNull(writer.BaseStream);
                    Assert.Same(stream, writer.BaseStream);
                }
        }
Пример #14
0
        public void WriteStringEncoding_Theory(String testString, Encoding encoding)
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    long   length     = writer.WriteString(testString, encoding);
                    byte[] buffer     = stream.GetBuffer();
                    byte[] testBuffer = new byte[length];
                    Buffer.BlockCopy(buffer, 0, testBuffer, 0, (int)length);
                    string result = encoding.GetString(testBuffer);
                    Assert.Equal(testString, result);
                    Assert.Equal <long>(length, stream.Position);
                    Assert.Equal <long>(stream.Position, writer.Position);
                }
        }
Пример #15
0
        public void WriteInt24_Theory(int test)
        {
            DjvuWriter writer = null;

            using (MemoryStream stream = new MemoryStream())
                using (writer = new DjvuWriter(stream))
                {
                    writer.WriteInt24(test);
                    byte[] buffer     = stream.GetBuffer();
                    byte[] testBuffer = new byte[4];
                    Buffer.BlockCopy(buffer, 0, testBuffer, 0, 3);
                    if ((buffer[2] >> 7) == 1)
                    {
                        testBuffer[3] = 0xff;
                    }
                    Assert.Equal <int>(test, BitConverter.ToInt32(testBuffer, 0));
                }
        }
Пример #16
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);
                //}
            }
        }
Пример #17
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);
                        }
Пример #18
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);
                            }
                        }
                    }
                }
            }
        }