public async Task CalculateStorageLength_ReturnsSameLengthAsSerializedResult(PixelStorageOptions storageOptions)
        {
            PixelStorageOptionsSerializer serializer = new PixelStorageOptionsSerializer();

            byte[] bytes = await serializer.SerializeToBytesAsync(storageOptions);

            Assert.AreEqual(bytes.Length, serializer.CalculateStorageLength(storageOptions.Channels.Count));
        }
示例#2
0
        protected override async Task <Stream> CreateImageWriterStreamAsync(ImageConfiguration configuration, Stream output, long totalBytes)
        {
            Imaging.ImageFormat imageFormat = ImageFormatFactory.GetFormat(configuration.Format);

            var pixelStorageSerializer = new PixelStorageOptionsSerializer();

            totalBytes += pixelStorageSerializer.CalculateStorageLength(imageFormat);

            ImageDimensions     embeddedImageDimensions = null;
            PixelStorageOptions pixelStorageOptions;
            Stream embeddedImageStream = null;

            if (configuration.HasEmbeddedImage)
            {
                embeddedImageDimensions = new ImageDimensions(configuration.EmbeddedImage.Image);
                pixelStorageOptions     = _pixelStorageCalculator.CalculatePixelStorageOptions(
                    imageFormat,
                    configuration.EmbeddedImage.EmbeddedPixelStorage,
                    embeddedImageDimensions,
                    totalBytes);

                embeddedImageStream = imageFormat.LoadPixelDataStream(configuration.EmbeddedImage.Image);
            }
            else
            {
                pixelStorageOptions = imageFormat.PixelStorageWithBitsPerChannel(
                    8,
                    PixelStorageOptions.BitStorageMode.MostSignificantBits);
            }

            byte[] pixelStorageBytes = await pixelStorageSerializer.SerializeToBytesAsync(pixelStorageOptions);

            int embeddedImageRepeats;

            ImageOptions imageOptions = GenerateImageOptions(
                configuration,
                imageFormat,
                CalculateImageDimensions(imageFormat, pixelStorageOptions, embeddedImageDimensions, totalBytes, out embeddedImageRepeats));

            Stream imageStream = imageFormat.CreateWriter(imageOptions).CreateOutputStream(output, true, EncodingConfiguration.BufferSize);

            await WriteHeaderAsync(imageStream);

            await imageStream.WriteAsync(pixelStorageBytes, 0, pixelStorageBytes.Length);

            if (configuration.HasEmbeddedImage)
            {
                var repeatingImageStream = new RepeatingStream(embeddedImageStream, embeddedImageRepeats);
                return(new PixelStorageWriterStream(
                           imageStream,
                           new SubStream(
                               repeatingImageStream,
                               imageStream.Position,
                               repeatingImageStream.Length - repeatingImageStream.Position - imageStream.Position),
                           pixelStorageOptions,
                           leaveOpen: false,
                           bufferSize: EncodingConfiguration.BufferSize));
            }

            return(imageStream);
        }