/// <summary> /// Writes a color image into a DjVu IW44 file. This function creates a /// composite Form element containing one or more IW44 encoded chunks. /// Data for each chunk is generated with a call to EncodeChunk method /// while encoding is controlled by the corresponding parameters in an array /// parameter settings. /// </summary> /// <param name="writer"></param> /// <param name="settings"></param> /// <returns></returns> public DjvuFormElement EncodeImage(IDjvuWriter writer, int nchunks, InterWaveEncoderSettings[] settings, ChunkType formType = ChunkType.PM44Form, ChunkType nodeType = ChunkType.PM44) { if (_YEncoder != null) { throw new DjvuInvalidOperationException($"Encoder already exists or left open from previous operation."); } int flag = 1; PM44Form form = (PM44Form)DjvuParser.CreateEncodedDjvuNode(writer, null, formType, 0); for (int i = 0; flag != 0 && i < nchunks; i++) { byte[] data = null; using (MemoryStream stream = new MemoryStream()) { flag = EncodeChunk(stream, settings[i]); data = new byte[stream.Position]; Buffer.BlockCopy(stream.GetBuffer(), 0, data, 0, data.Length); } PM44Chunk chunk = (PM44Chunk)DjvuParser.CreateEncodedDjvuNode(writer, form, nodeType, data.Length); chunk.ChunkData = data; form.AddChild(chunk); } CloseEncoder(); return(form); }
public void WriteDataTest() { byte[] buffer = new byte[2048]; Mock <IDjvuReader> readerMock = new Mock <IDjvuReader>(); readerMock.Setup(x => x.Position).Returns(1024); Mock <DjvuNode> nodeMock1 = new Mock <DjvuNode> { CallBase = true }; nodeMock1.Object.ChunkData = buffer; nodeMock1.Setup(x => x.Name).Returns("PM44"); nodeMock1.Object.Length = buffer.Length; Mock <DjvuNode> nodeMock2 = new Mock <DjvuNode> { CallBase = true }; nodeMock2.Object.ChunkData = buffer; nodeMock2.Setup(x => x.Name).Returns("PM44"); nodeMock2.Object.Length = buffer.Length; Mock <IDjvuWriter> writerMock = new Mock <IDjvuWriter>(); writerMock.Setup <long>(x => x.WriteUTF8String(It.IsAny <string>())).Returns(4); writerMock.Setup(x => x.WriteUInt32BigEndian(It.IsAny <uint>())); writerMock.Setup(x => x.Write(buffer, 0, buffer.Length)); PM44Form form = new PM44Form(readerMock.Object, null, null, "PM44Form", 2048); form.ChunkData = buffer; int result = form.AddChild(nodeMock1.Object); Assert.Equal <int>(1, result); Assert.Equal <int>(1, form.Children.Count); result = form.InsertChild(0, nodeMock2.Object); Assert.Equal <int>(2, result); Assert.Equal <int>(2, form.Children.Count); form.WriteData(writerMock.Object); writerMock.Verify(x => x.WriteUTF8String("FORM"), Times.Once()); writerMock.Verify(x => x.WriteUTF8String("PM44"), Times.Exactly(3)); writerMock.Verify(x => x.WriteUInt32BigEndian((uint)buffer.Length), Times.Exactly(2)); writerMock.Verify(x => x.WriteUInt32BigEndian(((uint)buffer.Length + 8) * 2 + 4), Times.Exactly(1)); writerMock.Verify(x => x.Write(buffer, 0, buffer.Length), Times.Exactly(2)); }