/// <inheritdoc/> public async Task <ProcessResultModel> ProcessAsync(ProcessPathCommand command, CancellationToken ct) { Ensure.ArgumentNotNull(command, nameof(command)); Ensure.ArgumentNotNull(ct, nameof(ct)); ImageFormat imageFormat = GetImageFormat(command.ImageFormat); string imageExtension = imageFormat.FileExtensionFromEncoder(); if (string.IsNullOrWhiteSpace(imageExtension)) { throw new UnexpectedNullException("Image file extension could not be determined."); } var fs = fileSystemStrategy.Create(command.DestinationPath); if (fs == null) { throw new UnexpectedNullException("Filesystem could not be created based on the destination path."); } var reader = readerFactory.Create(); if (reader == null) { throw new UnexpectedNullException("Image reader could not be created."); } await reader.InitAsync(command.SourcePath, ct); reader.Mapper = new AxisPositionMapper(command.AmountPerAxis, reader.Width, reader.Height, reader.Depth); PreProcess(reader, imageFormat, command.AmountPerAxis, command.OutputSize); var result = new ProcessResultModel { LabelCount = Convert.ToInt32(reader.GetLabelCount()), Size = new int[] { reader.Width, reader.Height, reader.Depth } }; ISet <AxisType> axisTypes = new HashSet <AxisType>(command.AxisTypes); if (axisTypes.Count == 0) { axisTypes = new HashSet <AxisType> { AxisType.Z }; } BitmapWrapper watermark = null; if (!string.IsNullOrWhiteSpace(command.WatermarkSourcePath)) { BitmapReader bitmapReader = new BitmapReader(); var watermarkBitmap = await bitmapReader.ReadAsync(command.WatermarkSourcePath, command.OutputSize, ct); if (watermarkBitmap == null) { throw new UnexpectedNullException("Watermark could not be read."); } watermark = new BitmapWrapper(watermarkBitmap); } var images = new List <PositionAxisContainerModel <string> >(); foreach (AxisType axisType in axisTypes) { for (int i = 0; i < reader.Mapper.GetLength(axisType); i++) { ct.ThrowIfCancellationRequested(); string filename = $"{axisType}_{i}{imageExtension}"; var image = WriteImage(i, fs, command, reader, axisType, imageFormat, filename, watermark); if (image != null) { images.Add(image); } } } reader.Dispose(); result.Images = images.OrderBy(e => e.Position).ToList(); return(result); }
/// <summary> /// Processes the images asynchronous. /// </summary> /// <param name="command">The command information.</param> /// <param name="ct">The cancellation token.</param> /// <returns> /// The result of the image processing. /// </returns> /// <exception cref="ArgumentNullException"> /// command /// or /// ct /// </exception> /// <exception cref="UnexpectedNullException"> /// Image file extension could not be determined. /// or /// Filesystem could not be created based on the destination path. /// or /// Image reader could not be created. /// or /// Watermark could not be read. /// or /// Bitmap could not be centered. /// </exception> public async Task <ProcessResultModel> ProcessAsync(ProcessPathCommand command, CancellationToken ct) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (ct == null) { throw new ArgumentNullException(nameof(ct)); } var result = new ProcessResultModel(); var imageFormat = GetImageFormat(command.ImageFormat); var imageExtension = imageFormat.FileExtensionFromEncoder(); if (string.IsNullOrWhiteSpace(imageExtension)) { throw new UnexpectedNullException("Image file extension could not be determined."); } var fs = fileSystemStrategy.Create(command.DestinationPath); if (fs == null) { throw new UnexpectedNullException("Filesystem could not be created based on the destination path."); } var reader = readerFactory.Create(); if (reader == null) { throw new UnexpectedNullException("Image reader could not be created."); } await reader.InitAsync(command.SourcePath, ct); reader.Mapper = new AxisPositionMapper(Convert.ToUInt32(command.AmountPerAxis), reader.Width, reader.Height, reader.Depth); PreProcess(reader, imageFormat, Convert.ToUInt32(command.AmountPerAxis), Convert.ToUInt32(command.DesiredSize)); result.LabelCount = Convert.ToInt32(reader.GetLabelCount()); ISet <AxisType> axisTypes = new HashSet <AxisType>(command.AxisTypes); if (axisTypes.Count == 0) { axisTypes = reader.GetRecommendedAxisTypes(); } BitmapWrapper watermark = null; if (!string.IsNullOrWhiteSpace(command.WatermarkSourcePath)) { BitmapReader bitmapReader = new BitmapReader(); var watermarkBitmap = await bitmapReader.ReadAsync(command.WatermarkSourcePath, Convert.ToUInt32(command.DesiredSize), ct); if (watermarkBitmap == null) { throw new UnexpectedNullException("Watermark could not be read."); } watermark = new BitmapWrapper(watermarkBitmap); } var images = new List <PositionAxisContainerModel <string> >(); foreach (AxisType axisType in axisTypes) { for (int i = 0; i < command.AmountPerAxis; i++) { ct.ThrowIfCancellationRequested(); string filename = $"{axisType}_{i}{imageExtension}"; var bitmap = reader.ExtractPosition(axisType, Convert.ToUInt32(i), Convert.ToUInt32(command.DesiredSize)); if (bitmap != null) { if (command.Grayscale) { bitmap = bitmap.To8bppIndexedGrayscale(); } bitmap = bitmap.ToCenter(Convert.ToUInt32(command.DesiredSize), Color.Black); if (bitmap == null) { throw new UnexpectedNullException("Bitmap could not be centered."); } if (watermark != null) { bitmap = bitmap.AppendWatermark(watermark); } fs.File.WriteAllBytes(fs.Path.Combine(command.DestinationPath, filename), bitmap.ToByteArray(imageFormat)); images.Add(new PositionAxisContainerModel <string>(Convert.ToUInt32(i), axisType, filename)); } } } result.Images = images.OrderBy(e => e.Position).ToList(); return(result); }