From 85fa64427a35978f74818905ec4569e133a504c1 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Wed, 3 Jan 2024 16:08:11 -0800 Subject: [PATCH] Remove BitDepth from ImageMetadata This is obsolete now that we have LogicalPixelFormat --- .../Recovery/RecoveryStorageManagerTests.cs | 3 -- NAPS2.Lib/Recovery/RecoverableFolder.cs | 2 +- NAPS2.Lib/Recovery/RecoveryIndexImage.cs | 2 -- NAPS2.Lib/Recovery/RecoveryStorageManager.cs | 1 - .../Images/ImageExportHelperTests.cs | 30 ++++++++-------- .../Images/ImageSerializerTests.cs | 2 -- NAPS2.Sdk.Tests/Images/ProcessedImageTests.cs | 16 ++++----- .../ImportExport/ImageImporterTests.cs | 4 --- NAPS2.Sdk.Tests/Pdf/PdfBenchmarkTests.cs | 12 +++---- NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs | 14 ++++---- NAPS2.Sdk/Images/ImageExportHelper.cs | 20 ++++------- NAPS2.Sdk/Images/ImageMetadata.cs | 6 ++-- NAPS2.Sdk/ImportExport/ImageImporter.cs | 1 - NAPS2.Sdk/Pdf/PdfExporter.cs | 2 +- NAPS2.Sdk/Pdf/PdfImporter.cs | 2 +- .../Scan/Internal/RemotePostProcessor.cs | 3 +- NAPS2.Sdk/Scan/ScanningContext.cs | 35 ++++++++----------- NAPS2.Sdk/Serialization/ImageSerializer.cs | 2 -- NAPS2.Sdk/Serialization/SerializedImage.proto | 5 ++- 19 files changed, 64 insertions(+), 98 deletions(-) diff --git a/NAPS2.Lib.Tests/Recovery/RecoveryStorageManagerTests.cs b/NAPS2.Lib.Tests/Recovery/RecoveryStorageManagerTests.cs index 03a526d5cc..0758b44818 100644 --- a/NAPS2.Lib.Tests/Recovery/RecoveryStorageManagerTests.cs +++ b/NAPS2.Lib.Tests/Recovery/RecoveryStorageManagerTests.cs @@ -52,7 +52,6 @@ public void IndexFileDefaultMetadata() Assert.Contains("00001.jpg", indexFileContent); Assert.Contains("00002.jpg", indexFileContent); - Assert.Contains("C24Bit", indexFileContent); Assert.Contains("false", indexFileContent); } @@ -79,14 +78,12 @@ public void IndexFileCustomMetadata() var image1 = new UiImage( ScanningContext.CreateProcessedImage( ImageContext.Create(100, 100, ImagePixelFormat.RGB24), - BitDepth.Grayscale, true, -1, PageSize.A4)); _imageList.Mutate(new ListMutation.Append(image1)); var indexFileContent = File.ReadAllText(Path.Combine(_recoveryFolder, "index.xml")); - Assert.Contains("Grayscale", indexFileContent); Assert.Contains("true", indexFileContent); Assert.Contains("210x297 mm", indexFileContent); } diff --git a/NAPS2.Lib/Recovery/RecoverableFolder.cs b/NAPS2.Lib/Recovery/RecoverableFolder.cs index e4e39946be..41654ff02f 100644 --- a/NAPS2.Lib/Recovery/RecoverableFolder.cs +++ b/NAPS2.Lib/Recovery/RecoverableFolder.cs @@ -151,7 +151,7 @@ public bool TryRecover(Action imageCallback, RecoveryParams reco private ProcessedImage CreateRecoveredImage(RecoveryParams recoveryParams, IImageStorage storage, RecoveryIndexImage indexImage) { - var processedImage = _scanningContext.CreateProcessedImage(storage, indexImage.BitDepth.ToBitDepth(), + var processedImage = _scanningContext.CreateProcessedImage(storage, indexImage.HighQuality, -1, PageSize.Parse(indexImage.PageSize), indexImage.TransformList!.ToImmutableList()); diff --git a/NAPS2.Lib/Recovery/RecoveryIndexImage.cs b/NAPS2.Lib/Recovery/RecoveryIndexImage.cs index 3e999a710a..4781ce8d0e 100644 --- a/NAPS2.Lib/Recovery/RecoveryIndexImage.cs +++ b/NAPS2.Lib/Recovery/RecoveryIndexImage.cs @@ -8,8 +8,6 @@ public class RecoveryIndexImage public List? TransformList { get; set; } - public ScanBitDepth BitDepth { get; set; } - public bool HighQuality { get; set; } public string? PageSize { get; set; } diff --git a/NAPS2.Lib/Recovery/RecoveryStorageManager.cs b/NAPS2.Lib/Recovery/RecoveryStorageManager.cs index e321660ac5..d6314fa374 100644 --- a/NAPS2.Lib/Recovery/RecoveryStorageManager.cs +++ b/NAPS2.Lib/Recovery/RecoveryStorageManager.cs @@ -91,7 +91,6 @@ private void WriteIndex(IEnumerable images) return new RecoveryIndexImage { FileName = Path.GetFileName(storage.FullPath), - BitDepth = processedImage.Metadata.BitDepth.ToScanBitDepth(), HighQuality = processedImage.Metadata.Lossless, PageSize = processedImage.Metadata.PageSize?.ToString(), TransformList = processedImage.TransformState.Transforms.ToList() diff --git a/NAPS2.Sdk.Tests/Images/ImageExportHelperTests.cs b/NAPS2.Sdk.Tests/Images/ImageExportHelperTests.cs index 8795591d2a..ffd0898cf1 100644 --- a/NAPS2.Sdk.Tests/Images/ImageExportHelperTests.cs +++ b/NAPS2.Sdk.Tests/Images/ImageExportHelperTests.cs @@ -18,7 +18,7 @@ public void SaveSmallestFormat_BlackAndWhite() var bw = LoadImage(ImageResources.dog_bw).PerformTransform(new BlackWhiteTransform()); var path = Path.Combine(FolderPath, "test"); - var fullPath = _helper.SaveSmallestFormat(path, bw, BitDepth.BlackAndWhite, false, -1, out var format); + var fullPath = _helper.SaveSmallestFormat(path, bw, false, -1, out var format); AssertPng(format, fullPath, ImageResources.dog_bw); } @@ -29,42 +29,42 @@ public void SaveSmallestFormat_BlackAndWhiteWithColorBitDepth() var bw = LoadImage(ImageResources.dog_bw).PerformTransform(new BlackWhiteTransform()); var path = Path.Combine(FolderPath, "test"); - var fullPath = _helper.SaveSmallestFormat(path, bw, BitDepth.Color, false, -1, out var format); + var fullPath = _helper.SaveSmallestFormat(path, bw, false, -1, out var format); AssertPng(format, fullPath, ImageResources.dog_bw); } [Fact] - public void SaveSmallestFormat_ColorWithBlackWhiteBitDepth() + public void SaveSmallestFormat_ColorHighQuality() { var color = LoadImage(ImageResources.dog); var path = Path.Combine(FolderPath, "test"); - var fullPath = _helper.SaveSmallestFormat(path, color, BitDepth.BlackAndWhite, false, -1, out var format); + var fullPath = _helper.SaveSmallestFormat(path, color, true, -1, out var format); - AssertPng(format, fullPath, ImageResources.dog_bw, ImageAsserts.XPLAT_RMSE_THRESHOLD); + AssertPng(format, fullPath, ImageResources.dog); } [Fact] - public void SaveSmallestFormat_ColorHighQuality() + public void SaveSmallestFormat_LogicalBlackWhite() { - var color = LoadImage(ImageResources.dog); + var bw = LoadImage(ImageResources.dog_bw_24bit); var path = Path.Combine(FolderPath, "test"); - var fullPath = _helper.SaveSmallestFormat(path, color, BitDepth.Color, true, -1, out var format); + var fullPath = _helper.SaveSmallestFormat(path, bw, false, -1, out var format); - AssertPng(format, fullPath, ImageResources.dog); + AssertPng(format, fullPath, ImageResources.dog_bw); } [Fact] public void SaveSmallestFormat_SmallerPng() { - var bw = LoadImage(ImageResources.dog_bw_24bit); + var bw = LoadImage(ImageResources.dog_clustered_gray); var path = Path.Combine(FolderPath, "test"); - var fullPath = _helper.SaveSmallestFormat(path, bw, BitDepth.Color, false, -1, out var format); + var fullPath = _helper.SaveSmallestFormat(path, bw, false, -1, out var format); - AssertPng(format, fullPath, ImageResources.dog_bw); + AssertPng(format, fullPath, ImageResources.dog_clustered_gray); } [Fact] @@ -73,7 +73,7 @@ public void SaveSmallestFormat_OriginalPng() var color = LoadImage(ImageResources.dog_png); var path = Path.Combine(FolderPath, "test"); - var fullPath = _helper.SaveSmallestFormat(path, color, BitDepth.Color, false, -1, out var format); + var fullPath = _helper.SaveSmallestFormat(path, color, false, -1, out var format); AssertPng(format, fullPath, ImageResources.dog); } @@ -85,7 +85,7 @@ public void SaveSmallestFormat_SmallerJpeg() color.OriginalFileFormat = ImageFileFormat.Unknown; var path = Path.Combine(FolderPath, "test"); - var fullPath = _helper.SaveSmallestFormat(path, color, BitDepth.Color, false, -1, out var format); + var fullPath = _helper.SaveSmallestFormat(path, color, false, -1, out var format); AssertJpeg(format, fullPath, ImageResources.dog); } @@ -96,7 +96,7 @@ public void SaveSmallestFormat_OriginalJpeg() var color = LoadImage(ImageResources.dog_bw_jpg); var path = Path.Combine(FolderPath, "test"); - var fullPath = _helper.SaveSmallestFormat(path, color, BitDepth.Color, false, -1, out var format); + var fullPath = _helper.SaveSmallestFormat(path, color, false, -1, out var format); AssertJpeg(format, fullPath, ImageResources.dog_bw); } diff --git a/NAPS2.Sdk.Tests/Images/ImageSerializerTests.cs b/NAPS2.Sdk.Tests/Images/ImageSerializerTests.cs index 4191eae41d..63e575fa9e 100644 --- a/NAPS2.Sdk.Tests/Images/ImageSerializerTests.cs +++ b/NAPS2.Sdk.Tests/Images/ImageSerializerTests.cs @@ -21,7 +21,6 @@ public void SerializesMetadata(StorageConfig config) using var sourceImage = ScanningContext.CreateProcessedImage( LoadImage(ImageResources.dog), // TODO: Use an actual grayscale image - BitDepth.Grayscale, true, -1, PageSize.A4, @@ -33,7 +32,6 @@ public void SerializesMetadata(StorageConfig config) Assert.Single(destImage.TransformState.Transforms); Assert.Equal(300, Assert.IsType(destImage.TransformState.Transforms[0]).Brightness); Assert.True(destImage.Metadata.Lossless); - Assert.Equal(BitDepth.Grayscale, destImage.Metadata.BitDepth); Assert.Equal(PageSize.A4, destImage.Metadata.PageSize); ImageAsserts.Similar(ImageResources.dog_b_p300, destImage); } diff --git a/NAPS2.Sdk.Tests/Images/ProcessedImageTests.cs b/NAPS2.Sdk.Tests/Images/ProcessedImageTests.cs index 844d6097de..18c74ae218 100644 --- a/NAPS2.Sdk.Tests/Images/ProcessedImageTests.cs +++ b/NAPS2.Sdk.Tests/Images/ProcessedImageTests.cs @@ -11,21 +11,19 @@ public void Construct() { var storage = LoadImage(ImageResources.dog); - var metadata1 = new ImageMetadata(BitDepth.Color, false, null); + var metadata1 = new ImageMetadata(false, null); var postProcessingData1 = new PostProcessingData(); var transformState1 = TransformState.Empty; var image1 = new ProcessedImage(ImageContext, storage, metadata1, postProcessingData1, transformState1); Assert.Equal(storage, image1.Storage); - Assert.Equal(BitDepth.Color, image1.Metadata.BitDepth); Assert.False(image1.Metadata.Lossless); Assert.True(image1.TransformState.IsEmpty); - var metadata2 = new ImageMetadata(BitDepth.BlackAndWhite, true, null); + var metadata2 = new ImageMetadata(true, null); var postProcessingData2 = new PostProcessingData(); var transformState2 = new TransformState(ImmutableList.Empty.Add(new CropTransform(0, 50, 0, 50))); var image2 = new ProcessedImage(ImageContext, storage, metadata2, postProcessingData2, transformState2); Assert.Equal(storage, image2.Storage); - Assert.Equal(BitDepth.BlackAndWhite, image2.Metadata.BitDepth); Assert.True(image2.Metadata.Lossless); Assert.Single(image2.TransformState.Transforms); var cropTransform = Assert.IsType(image2.TransformState.Transforms[0]); @@ -39,7 +37,7 @@ public void Construct() public void StorageDisposed() { var storageMock = Substitute.For(); - var metadata = new ImageMetadata(BitDepth.Color, false, null); + var metadata = new ImageMetadata(false, null); var image = new ProcessedImage( ImageContext, storageMock, metadata, new PostProcessingData(), TransformState.Empty); @@ -52,7 +50,7 @@ public void StorageDisposed() public void StorageDisposedOnlyAfterAllClonesDisposed() { var storageMock = Substitute.For(); - var metadata = new ImageMetadata(BitDepth.Color, false, null); + var metadata = new ImageMetadata(false, null); var image = new ProcessedImage( ImageContext, storageMock, metadata, new PostProcessingData(), TransformState.Empty); @@ -80,7 +78,7 @@ public void StorageDisposedOnlyAfterAllClonesDisposed() public void TransformSimplification() { var storageMock = Substitute.For(); - var metadata = new ImageMetadata(BitDepth.Color, false, null); + var metadata = new ImageMetadata(false, null); var image = new ProcessedImage( ImageContext, storageMock, metadata, new PostProcessingData(), TransformState.Empty); @@ -118,7 +116,7 @@ public void TransformSimplification() public void MultipleTransforms() { var storageMock = Substitute.For(); - var metadata = new ImageMetadata(BitDepth.Color, false, null); + var metadata = new ImageMetadata(false, null); var image = new ProcessedImage( ImageContext, storageMock, metadata, new PostProcessingData(), TransformState.Empty); @@ -157,7 +155,7 @@ public void MultipleTransforms() public void CloneAfterDisposed() { var storageMock = Substitute.For(); - var metadata = new ImageMetadata(BitDepth.Color, false, null); + var metadata = new ImageMetadata(false, null); var image = new ProcessedImage( ImageContext, storageMock, metadata, new PostProcessingData(), TransformState.Empty); diff --git a/NAPS2.Sdk.Tests/ImportExport/ImageImporterTests.cs b/NAPS2.Sdk.Tests/ImportExport/ImageImporterTests.cs index 3239513f8c..0c7161acae 100644 --- a/NAPS2.Sdk.Tests/ImportExport/ImageImporterTests.cs +++ b/NAPS2.Sdk.Tests/ImportExport/ImageImporterTests.cs @@ -32,7 +32,6 @@ public async Task ImportPngImage() Assert.True(File.Exists(storage.FullPath)); Assert.Equal(Path.Combine(FolderPath, "recovery"), Path.GetDirectoryName(storage.FullPath)); Assert.True(result[0].Metadata.Lossless); - Assert.Equal(BitDepth.Color, result[0].Metadata.BitDepth); Assert.Null(result[0].PostProcessingData.Thumbnail); Assert.False(result[0].PostProcessingData.Barcode.IsDetectionAttempted); Assert.True(result[0].TransformState.IsEmpty); @@ -54,7 +53,6 @@ public async Task ImportJpegImage() Assert.True(File.Exists(storage.FullPath)); Assert.Equal(Path.Combine(FolderPath, "recovery"), Path.GetDirectoryName(storage.FullPath)); Assert.False(result[0].Metadata.Lossless); - Assert.Equal(BitDepth.Color, result[0].Metadata.BitDepth); Assert.Null(result[0].PostProcessingData.Thumbnail); Assert.False(result[0].PostProcessingData.Barcode.IsDetectionAttempted); Assert.True(result[0].TransformState.IsEmpty); @@ -79,12 +77,10 @@ public async Task ImportTiffImage() Assert.Equal(3, result.Count); AssertUsesRecoveryStorage(result[0].Storage, "00001.jpg"); Assert.False(result[0].Metadata.Lossless); - Assert.Equal(BitDepth.Color, result[0].Metadata.BitDepth); ImageAsserts.Similar(ImageResources.dog, result[0]); AssertUsesRecoveryStorage(result[2].Storage, "00003.jpg"); Assert.False(result[2].Metadata.Lossless); - Assert.Equal(BitDepth.Color, result[2].Metadata.BitDepth); ImageAsserts.Similar(ImageResources.stock_cat, result[2]); result[0].Dispose(); diff --git a/NAPS2.Sdk.Tests/Pdf/PdfBenchmarkTests.cs b/NAPS2.Sdk.Tests/Pdf/PdfBenchmarkTests.cs index 31267b499a..61e2db8142 100644 --- a/NAPS2.Sdk.Tests/Pdf/PdfBenchmarkTests.cs +++ b/NAPS2.Sdk.Tests/Pdf/PdfBenchmarkTests.cs @@ -9,7 +9,7 @@ public class PdfBenchmarkTests : ContextualTests public async Task PdfSharpExport300() { var filePath = Path.Combine(FolderPath, "test"); - using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog), BitDepth.Color, + using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog), false, -1, null, Enumerable.Empty()); var pdfExporter = new PdfExporter(ScanningContext); @@ -24,7 +24,7 @@ public async Task PdfSharpExportHuge() { var filePath = Path.Combine(FolderPath, "test"); using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog_huge), - BitDepth.Color, false, -1, null, Enumerable.Empty()); + false, -1, null, Enumerable.Empty()); var pdfExporter = new PdfExporter(ScanningContext); await pdfExporter.Export(filePath + ".pdf", new[] { image }); @@ -35,7 +35,7 @@ public async Task PdfSharpExportHugePng() { var filePath = Path.Combine(FolderPath, "test"); using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog_huge_png), - BitDepth.Color, true, -1, null, Enumerable.Empty()); + true, -1, null, Enumerable.Empty()); var pdfExporter = new PdfExporter(ScanningContext); await pdfExporter.Export(filePath + ".pdf", new[] { image }); @@ -45,7 +45,7 @@ public async Task PdfSharpExportHugePng() public async Task PdfiumExport300() { var filePath = Path.Combine(FolderPath, "test"); - using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog), BitDepth.Color, + using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog), false, -1, null, Enumerable.Empty()); var pdfExporter = new PdfiumPdfExporter(ScanningContext); @@ -60,7 +60,7 @@ public async Task PdfiumExportHuge() { var filePath = Path.Combine(FolderPath, "test"); using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog_huge), - BitDepth.Color, false, -1, null, Enumerable.Empty()); + false, -1, null, Enumerable.Empty()); var pdfExporter = new PdfiumPdfExporter(ScanningContext); await pdfExporter.Export(filePath + ".pdf", new[] { image }); @@ -71,7 +71,7 @@ public async Task PdfiumExportHugePng() { var filePath = Path.Combine(FolderPath, "test"); using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog_huge_png), - BitDepth.Color, true, -1, null, Enumerable.Empty()); + true, -1, null, Enumerable.Empty()); var pdfExporter = new PdfiumPdfExporter(ScanningContext); await pdfExporter.Export(filePath + ".pdf", new[] { image }); diff --git a/NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs b/NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs index 0a658ce5e3..b547d41e2a 100644 --- a/NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs +++ b/NAPS2.Sdk.Tests/Pdf/PdfExportTests.cs @@ -101,7 +101,7 @@ public async Task ExportPngImage(StorageConfig storageConfig) var filePath = Path.Combine(FolderPath, "test.pdf"); using var image = ScanningContext.CreateProcessedImage( - LoadImage(ImageResources.dog_png), BitDepth.Color, true, -1, null); + LoadImage(ImageResources.dog_png), true, -1, null); await _exporter.Export(filePath, new[] { image }); @@ -118,7 +118,7 @@ public async Task ExportUnalignedPngImage(StorageConfig storageConfig) var filePath = Path.Combine(FolderPath, "test.pdf"); // Width is 99 (not divisible by 4) var image = ScanningContext.CreateProcessedImage( - LoadImage(ImageResources.dog_99w), BitDepth.Color, true, -1, null); + LoadImage(ImageResources.dog_99w), true, -1, null); await _exporter.Export(filePath, new[] { image }); @@ -134,7 +134,7 @@ public async Task ExportAlphaImage(StorageConfig storageConfig) var filePath = Path.Combine(FolderPath, "test.pdf"); using var image = ScanningContext.CreateProcessedImage( - LoadImage(ImageResources.dog_alpha), BitDepth.Color, false, -1, null); + LoadImage(ImageResources.dog_alpha), false, -1, null); await _exporter.Export(filePath, new[] { image }); @@ -151,7 +151,7 @@ public async Task ExportMaskedImage(StorageConfig storageConfig) var filePath = Path.Combine(FolderPath, "test.pdf"); using var image = ScanningContext.CreateProcessedImage( - LoadImage(ImageResources.dog_mask), BitDepth.Color, false, -1, null); + LoadImage(ImageResources.dog_mask), false, -1, null); await _exporter.Export(filePath, new[] { image }); @@ -218,7 +218,7 @@ public async Task ExportBlackAndWhiteImageByMetadata(StorageConfig storageConfig var filePath = Path.Combine(FolderPath, "test.pdf"); using var image = ScanningContext.CreateProcessedImage(LoadImage(ImageResources.dog_bw_24bit), - BitDepth.BlackAndWhite, true, -1, null); + true, -1, null); await _exporter.Export(filePath, new[] { image }); @@ -426,7 +426,7 @@ public async Task ExportWithPageSize(StorageConfig storageConfig) var filePath = Path.Combine(FolderPath, "test.pdf"); var sourceImage = ImageContext.Create(850, 1100, ImagePixelFormat.RGB24); sourceImage.SetResolution(99.4f, 99.4f); - using var image = ScanningContext.CreateProcessedImage(sourceImage, BitDepth.Color, false, -1, PageSize.Letter); + using var image = ScanningContext.CreateProcessedImage(sourceImage, false, -1, PageSize.Letter); await _exporter.Export(filePath, new[] { image }); @@ -443,7 +443,7 @@ public async Task ExportWithMismatchedPageSize(StorageConfig storageConfig) var filePath = Path.Combine(FolderPath, "test.pdf"); var sourceImage = ImageContext.Create(850, 1100, ImagePixelFormat.RGB24); sourceImage.SetResolution(98, 98); - using var image = ScanningContext.CreateProcessedImage(sourceImage, BitDepth.Color, false, -1, PageSize.Letter); + using var image = ScanningContext.CreateProcessedImage(sourceImage, false, -1, PageSize.Letter); await _exporter.Export(filePath, new[] { image }); diff --git a/NAPS2.Sdk/Images/ImageExportHelper.cs b/NAPS2.Sdk/Images/ImageExportHelper.cs index 8cdd396fcc..6628b250fb 100644 --- a/NAPS2.Sdk/Images/ImageExportHelper.cs +++ b/NAPS2.Sdk/Images/ImageExportHelper.cs @@ -2,11 +2,11 @@ namespace NAPS2.Images; internal class ImageExportHelper { - public string SaveSmallestFormat(string pathWithoutExtension, IMemoryImage image, BitDepth bitDepth, + public string SaveSmallestFormat(string pathWithoutExtension, IMemoryImage image, bool lossless, int quality, out ImageFileFormat imageFileFormat) { // TODO: Should we save directly to the file? - var memoryStream = SaveSmallestFormatToMemoryStream(image, bitDepth, lossless, quality, out imageFileFormat); + var memoryStream = SaveSmallestFormatToMemoryStream(image, lossless, quality, out imageFileFormat); var ext = imageFileFormat == ImageFileFormat.Png ? ".png" : ".jpg"; var path = pathWithoutExtension + ext; using var fileStream = new FileStream(path, FileMode.Create); @@ -14,10 +14,10 @@ public string SaveSmallestFormat(string pathWithoutExtension, IMemoryImage image return path; } - public MemoryStream SaveSmallestFormatToMemoryStream(IMemoryImage image, BitDepth bitDepth, bool lossless, + public MemoryStream SaveSmallestFormatToMemoryStream(IMemoryImage image, bool lossless, int quality, out ImageFileFormat imageFileFormat) { - var exportFormat = GetExportFormat(image, bitDepth, lossless); + var exportFormat = GetExportFormat(image, lossless); if (exportFormat.FileFormat == ImageFileFormat.Png) { imageFileFormat = ImageFileFormat.Png; @@ -47,21 +47,13 @@ public MemoryStream SaveSmallestFormatToMemoryStream(IMemoryImage image, BitDept return jpegEncoded; } - public ImageExportFormat GetExportFormat(IMemoryImage image, BitDepth bitDepth, bool lossless) + public ImageExportFormat GetExportFormat(IMemoryImage image, bool lossless) { image.UpdateLogicalPixelFormat(); // Store the image in as little space as possible if (image.LogicalPixelFormat == ImagePixelFormat.BW1) { - // Already encoded as 1-bit - return new ImageExportFormat(ImageFileFormat.Png, ImagePixelFormat.BW1); - } - if (bitDepth == BitDepth.BlackAndWhite) - { - // Convert to a 1-bit bitmap before saving to help compression - // This is lossless and takes up minimal storage (best of both worlds), so highQuality is irrelevant - // Note that if a black and white image comes from native WIA, bitDepth is unknown, - // so the image will be png-encoded below instead of using a 1-bit bitmap + // Already 1-bit return new ImageExportFormat(ImageFileFormat.Png, ImagePixelFormat.BW1); } if (lossless || image.LogicalPixelFormat == ImagePixelFormat.ARGB32 || diff --git a/NAPS2.Sdk/Images/ImageMetadata.cs b/NAPS2.Sdk/Images/ImageMetadata.cs index e86d55f56e..ad991c11b4 100644 --- a/NAPS2.Sdk/Images/ImageMetadata.cs +++ b/NAPS2.Sdk/Images/ImageMetadata.cs @@ -1,12 +1,12 @@ namespace NAPS2.Images; /// -/// Represents additional information about a scanned image (bit depth, quality, page size). +/// Represents additional information about a scanned image (quality, page size). /// -public record ImageMetadata(BitDepth BitDepth, bool Lossless, PageSize? PageSize) +public record ImageMetadata(bool Lossless, PageSize? PageSize) { /// /// A default set of metadata suitable for test images. Real use cases should be explicit and not use this default value. /// - public static readonly ImageMetadata DefaultForTesting = new(BitDepth.Color, false, null); + public static readonly ImageMetadata DefaultForTesting = new(false, null); } diff --git a/NAPS2.Sdk/ImportExport/ImageImporter.cs b/NAPS2.Sdk/ImportExport/ImageImporter.cs index 1a17c1acf0..9ce8122457 100644 --- a/NAPS2.Sdk/ImportExport/ImageImporter.cs +++ b/NAPS2.Sdk/ImportExport/ImageImporter.cs @@ -48,7 +48,6 @@ public IAsyncEnumerable Import(string filePath, ImportParams? im frame.OriginalFileFormat == ImageFileFormat.Jpeg ? CreateJpegStorageWithoutReEncoding(filePath, frame) : frame, - BitDepth.Color, lossless, -1, null); diff --git a/NAPS2.Sdk/Pdf/PdfExporter.cs b/NAPS2.Sdk/Pdf/PdfExporter.cs index 9f739a5819..67b0f9ede3 100644 --- a/NAPS2.Sdk/Pdf/PdfExporter.cs +++ b/NAPS2.Sdk/Pdf/PdfExporter.cs @@ -704,7 +704,7 @@ public void CopyToStream(Stream stream) public ImageExportFormat PrepareForExport(ImageMetadata metadata) { - var exportFormat = new ImageExportHelper().GetExportFormat(Image, metadata.BitDepth, metadata.Lossless); + var exportFormat = new ImageExportHelper().GetExportFormat(Image, metadata.Lossless); if (exportFormat.FileFormat == ImageFileFormat.Unknown) { exportFormat = exportFormat with { FileFormat = ImageFileFormat.Jpeg }; diff --git a/NAPS2.Sdk/Pdf/PdfImporter.cs b/NAPS2.Sdk/Pdf/PdfImporter.cs index 880aee3a04..a3884d940e 100644 --- a/NAPS2.Sdk/Pdf/PdfImporter.cs +++ b/NAPS2.Sdk/Pdf/PdfImporter.cs @@ -101,7 +101,7 @@ private ProcessedImage GetImageFromPage(PdfPage page, ImportParams importParams) if (storage != null) { var pageSize = new PageSize((decimal) page.Width * 72, (decimal) page.Height * 72, PageSizeUnit.Inch); - var image = _scanningContext.CreateProcessedImage(storage, BitDepth.Color, false, -1, pageSize); + var image = _scanningContext.CreateProcessedImage(storage, false, -1, pageSize); return ImportPostProcessor.AddPostProcessingData( image, storage, diff --git a/NAPS2.Sdk/Scan/Internal/RemotePostProcessor.cs b/NAPS2.Sdk/Scan/Internal/RemotePostProcessor.cs index cdd378c03e..f303016869 100644 --- a/NAPS2.Sdk/Scan/Internal/RemotePostProcessor.cs +++ b/NAPS2.Sdk/Scan/Internal/RemotePostProcessor.cs @@ -48,8 +48,7 @@ public RemotePostProcessor(ScanningContext scanningContext) } } - var bitDepth = options.UseNativeUI ? BitDepth.Color : options.BitDepth; - var scannedImage = _scanningContext.CreateProcessedImage(image, bitDepth, options.MaxQuality, + var scannedImage = _scanningContext.CreateProcessedImage(image, options.MaxQuality, options.Quality, options.PageSize); DoRevertibleTransforms(ref scannedImage, ref image, options, postProcessingContext); postProcessingContext.TempPath = SaveForBackgroundOcr(image, options); diff --git a/NAPS2.Sdk/Scan/ScanningContext.cs b/NAPS2.Sdk/Scan/ScanningContext.cs index f16bc2eade..0201191321 100644 --- a/NAPS2.Sdk/Scan/ScanningContext.cs +++ b/NAPS2.Sdk/Scan/ScanningContext.cs @@ -87,27 +87,20 @@ internal ProcessedImage CreateProcessedImage(IImageStorage storage) internal ProcessedImage CreateProcessedImage(IImageStorage storage, IEnumerable transforms) { - (storage as IMemoryImage)?.UpdateLogicalPixelFormat(); - var bitDepth = storage switch - { - IMemoryImage { LogicalPixelFormat: ImagePixelFormat.BW1 } => BitDepth.BlackAndWhite, - IMemoryImage { LogicalPixelFormat: ImagePixelFormat.Gray8 } => BitDepth.Grayscale, - _ => BitDepth.Color - }; - return CreateProcessedImage(storage, bitDepth, false, -1, null, transforms); + return CreateProcessedImage(storage, false, -1, null, transforms); } - internal ProcessedImage CreateProcessedImage(IImageStorage storage, BitDepth bitDepth, bool lossless, int quality, + internal ProcessedImage CreateProcessedImage(IImageStorage storage, bool lossless, int quality, PageSize? pageSize) { - return CreateProcessedImage(storage, bitDepth, lossless, quality, pageSize, Enumerable.Empty()); + return CreateProcessedImage(storage, lossless, quality, pageSize, Enumerable.Empty()); } - internal ProcessedImage CreateProcessedImage(IImageStorage storage, BitDepth bitDepth, bool lossless, int quality, + internal ProcessedImage CreateProcessedImage(IImageStorage storage, bool lossless, int quality, PageSize? pageSize, IEnumerable transforms) { - var convertedStorage = ConvertStorageIfNeeded(storage, bitDepth, lossless, quality); - var metadata = new ImageMetadata(bitDepth, lossless, pageSize); + var convertedStorage = ConvertStorageIfNeeded(storage, lossless, quality); + var metadata = new ImageMetadata(lossless, pageSize); var image = new ProcessedImage( ImageContext, convertedStorage, @@ -118,11 +111,11 @@ internal ProcessedImage CreateProcessedImage(IImageStorage storage, BitDepth bit return image; } - private IImageStorage ConvertStorageIfNeeded(IImageStorage storage, BitDepth bitDepth, bool lossless, int quality) + private IImageStorage ConvertStorageIfNeeded(IImageStorage storage, bool lossless, int quality) { if (FileStorageManager != null) { - return ConvertToFileStorage(storage, bitDepth, lossless, quality); + return ConvertToFileStorage(storage, lossless, quality); } return ConvertToMemoryStorage(storage); } @@ -147,12 +140,12 @@ private IImageStorage ConvertToMemoryStorage(IImageStorage storage) } } - private IImageStorage ConvertToFileStorage(IImageStorage storage, BitDepth bitDepth, bool lossless, int quality) + private IImageStorage ConvertToFileStorage(IImageStorage storage, bool lossless, int quality) { switch (storage) { case IMemoryImage image: - return WriteImageToBackingFile(image, bitDepth, lossless, quality); + return WriteImageToBackingFile(image, lossless, quality); case ImageFileStorage fileStorage: return fileStorage; case ImageMemoryStorage memoryStorage: @@ -162,7 +155,7 @@ private IImageStorage ConvertToFileStorage(IImageStorage storage, BitDepth bitDe } // TODO: Can we just write this to a file directly? Is there any case where SaveSmallestFormat is really needed? var loadedImage = ImageContext.Load(memoryStorage.Stream); - return WriteImageToBackingFile(loadedImage, bitDepth, lossless, quality); + return WriteImageToBackingFile(loadedImage, lossless, quality); default: // The only case that should hit this is a test with a mock return storage; @@ -181,7 +174,7 @@ private ImageFileStorage WriteDataToBackingFile(MemoryStream stream, string ext) return new ImageFileStorage(path, false); } - private IImageStorage WriteImageToBackingFile(IMemoryImage image, BitDepth bitDepth, bool lossless, int quality) + private IImageStorage WriteImageToBackingFile(IMemoryImage image, bool lossless, int quality) { if (FileStorageManager == null) { @@ -189,7 +182,7 @@ private IImageStorage WriteImageToBackingFile(IMemoryImage image, BitDepth bitDe } var path = FileStorageManager.NextFilePath(); var fullPath = new ImageExportHelper() - .SaveSmallestFormat(path, image, bitDepth, lossless, quality, out _); + .SaveSmallestFormat(path, image, lossless, quality, out _); return new ImageFileStorage(fullPath, false); } @@ -197,7 +190,7 @@ internal string SaveToTempFile(IMemoryImage image, BitDepth bitDepth = BitDepth. { var path = Path.Combine(TempFolderPath, Path.GetRandomFileName()); return new ImageExportHelper() - .SaveSmallestFormat(path, image, bitDepth, false, -1, out _); + .SaveSmallestFormat(path, image, false, -1, out _); } internal string SaveToTempFile(ProcessedImage image, BitDepth bitDepth = BitDepth.Color) diff --git a/NAPS2.Sdk/Serialization/ImageSerializer.cs b/NAPS2.Sdk/Serialization/ImageSerializer.cs index b67dc5cd0c..3fc0b4cfd3 100644 --- a/NAPS2.Sdk/Serialization/ImageSerializer.cs +++ b/NAPS2.Sdk/Serialization/ImageSerializer.cs @@ -37,7 +37,6 @@ public static SerializedImage Serialize(ProcessedImage image, SerializeImageOpti Metadata = new SerializedImageMetadata { TransformListXml = image.TransformState.Transforms.ToXml(), - BitDepth = (SerializedImageMetadata.Types.BitDepth) image.Metadata.BitDepth, Lossless = image.Metadata.Lossless, PageSize = image.Metadata.PageSize?.ToString() ?? "" }, @@ -142,7 +141,6 @@ public static ProcessedImage Deserialize(ScanningContext scanningContext, Serial var processedImage = scanningContext.CreateProcessedImage( storage, - (BitDepth) serializedImage.Metadata.BitDepth, serializedImage.Metadata.Lossless, -1, PageSize.Parse(serializedImage.Metadata.PageSize), diff --git a/NAPS2.Sdk/Serialization/SerializedImage.proto b/NAPS2.Sdk/Serialization/SerializedImage.proto index a25607ed12..8e21522626 100644 --- a/NAPS2.Sdk/Serialization/SerializedImage.proto +++ b/NAPS2.Sdk/Serialization/SerializedImage.proto @@ -22,7 +22,6 @@ message SerializedImageMetadata { Grayscale = 1; BlackAndWhite = 2; } - BitDepth bitDepth = 2; - bool lossless = 3; - string pageSize = 4; + bool lossless = 2; + string pageSize = 3; }