using System.Windows.Media.Imaging;

namespace Valg.EVA.Skanning.Tools
{
    using System;
    using System.IO;

    
    
    
    public class TiffConverter : ITiffConverter
    {
        
        
        
        
        
        public void CombineImages(Stream[] imageStreams, Stream outputStream)
        {
            if (imageStreams == null)
                throw new ArgumentNullException("imageStreams");

            if (imageStreams.Length < 1)
                throw new ArgumentException("No image streams specified", "imageStreams");

            var enc = new TiffBitmapEncoder();
            foreach (var imageStream in imageStreams)
            {
                enc.Frames.Add(BitmapFrame.Create(imageStream));
            }
            
            enc.Save(outputStream);
        }

        public byte[] GetCombinedImage(byte[] imageFront, byte[] imageBack)
        {
            byte[] combinedImage;
            using (var outputSteam = new MemoryStream())
            {
                var imageFrontStream = new MemoryStream(imageFront);
                var imageBackStream = new MemoryStream(imageBack);
                CombineImages(new Stream[] { imageFrontStream, imageBackStream }, outputSteam);
                combinedImage = outputSteam.ToArray();
                imageFrontStream.Dispose();
                imageBackStream.Dispose();
            }
            return combinedImage;
        }
    }
}
