naps2/NAPS2.Sdk/ImportExport/SaveSeparatorHelper.cs

73 lines
2.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using NAPS2.Scan;
using NAPS2.Images;
namespace NAPS2.ImportExport
{
public static class SaveSeparatorHelper
{
/// <summary>
/// Given a list of scans (each of which is a list of 1 or more images),
/// split up the images into multiple lists as described by the SaveSeparator parameter.
/// </summary>
/// <param name="scans"></param>
/// <param name="separator"></param>
2018-03-02 03:32:54 +03:00
/// <param name="splitSize"></param>
/// <returns></returns>
2018-03-02 03:32:54 +03:00
public static IEnumerable<List<ScannedImage>> SeparateScans(IEnumerable<IEnumerable<ScannedImage>> scans, SaveSeparator separator, int splitSize = 1)
{
if (separator == SaveSeparator.FilePerScan)
{
foreach (var scan in scans)
{
yield return scan.ToList();
}
}
else if (separator == SaveSeparator.FilePerPage)
{
2018-03-02 03:32:54 +03:00
splitSize = Math.Max(splitSize, 1);
foreach (var scan in scans.Select(x => x.ToList()))
{
2018-03-02 03:32:54 +03:00
for (int i = 0; i < scan.Count; i += splitSize)
{
2018-03-02 03:32:54 +03:00
yield return scan.Skip(i).Take(splitSize).ToList();
}
}
}
else if (separator == SaveSeparator.PatchT)
{
var images = new List<ScannedImage>();
foreach (var scan in scans)
{
foreach (var image in scan)
{
if (image.PatchCode == PatchCode.PatchT)
{
2016-07-03 05:12:30 +03:00
image.Dispose();
if (images.Count > 0)
{
yield return images;
images = new List<ScannedImage>();
}
}
else
{
images.Add(image);
}
}
2016-07-03 05:12:30 +03:00
}
if (images.Count > 0)
{
yield return images;
}
}
else
{
yield return scans.SelectMany(x => x.ToList()).ToList();
}
}
}
}