diff --git a/ComicCompress.csproj b/ComicCompress.csproj index bb552af..52f0c88 100644 --- a/ComicCompress.csproj +++ b/ComicCompress.csproj @@ -6,9 +6,17 @@ ComicCompressor ComicCompress True + Debug;Release;ReleaseWDbg + + + + + + + @@ -16,6 +24,7 @@ + diff --git a/ComicCompress.csproj.user b/ComicCompress.csproj.user index 9adce7d..d6f97a1 100644 --- a/ComicCompress.csproj.user +++ b/ComicCompress.csproj.user @@ -4,10 +4,12 @@ ProjectDebugger - DEV_HOME - <_LastSelectedProfileId>E:\_COMIX\_TOOLS_\comic-compress-new\Properties\PublishProfiles\FolderProfile.pubxml + ComicCompress CBx ProjectDebugger + + ProjectDebugger + \ No newline at end of file diff --git a/ComicCompress.sln b/ComicCompress.sln index a8d0cf7..be124de 100644 --- a/ComicCompress.sln +++ b/ComicCompress.sln @@ -13,20 +13,27 @@ Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU + ReleaseWDbg|Any CPU = ReleaseWDbg|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {2625C71D-E4EC-4768-BF04-87AAB364916A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2625C71D-E4EC-4768-BF04-87AAB364916A}.Debug|Any CPU.Build.0 = Debug|Any CPU {2625C71D-E4EC-4768-BF04-87AAB364916A}.Release|Any CPU.ActiveCfg = Release|Any CPU {2625C71D-E4EC-4768-BF04-87AAB364916A}.Release|Any CPU.Build.0 = Release|Any CPU + {2625C71D-E4EC-4768-BF04-87AAB364916A}.ReleaseWDbg|Any CPU.ActiveCfg = ReleaseWDbg|Any CPU + {2625C71D-E4EC-4768-BF04-87AAB364916A}.ReleaseWDbg|Any CPU.Build.0 = ReleaseWDbg|Any CPU {7330E00C-6C24-44B3-9768-1604802532FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {7330E00C-6C24-44B3-9768-1604802532FB}.Debug|Any CPU.Build.0 = Debug|Any CPU {7330E00C-6C24-44B3-9768-1604802532FB}.Release|Any CPU.ActiveCfg = Release|Any CPU {7330E00C-6C24-44B3-9768-1604802532FB}.Release|Any CPU.Build.0 = Release|Any CPU + {7330E00C-6C24-44B3-9768-1604802532FB}.ReleaseWDbg|Any CPU.ActiveCfg = Release|Any CPU + {7330E00C-6C24-44B3-9768-1604802532FB}.ReleaseWDbg|Any CPU.Build.0 = Release|Any CPU {990E9DC1-B842-4E0E-AD7F-00CB2EF24AF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {990E9DC1-B842-4E0E-AD7F-00CB2EF24AF4}.Debug|Any CPU.Build.0 = Debug|Any CPU {990E9DC1-B842-4E0E-AD7F-00CB2EF24AF4}.Release|Any CPU.ActiveCfg = Release|Any CPU {990E9DC1-B842-4E0E-AD7F-00CB2EF24AF4}.Release|Any CPU.Build.0 = Release|Any CPU + {990E9DC1-B842-4E0E-AD7F-00CB2EF24AF4}.ReleaseWDbg|Any CPU.ActiveCfg = Release|Any CPU + {990E9DC1-B842-4E0E-AD7F-00CB2EF24AF4}.ReleaseWDbg|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Compressor.cs b/Compressor.cs deleted file mode 100644 index 02cc503..0000000 --- a/Compressor.cs +++ /dev/null @@ -1,475 +0,0 @@ -using System; -using System.Linq; -using System.Drawing; -using Imazen.WebP; -using SharpCompress.Archives; -using SharpCompress.Archives.Rar; -using SharpCompress.Archives.Zip; -using System.IO; -using SharpCompress.Common; -using System.Threading.Tasks; -using System.Collections.Generic; -using System.Diagnostics; -using javax.swing.plaf; -using SharpCompress.Readers; -using SharpCompress.Writers; -using SharpCompress.Compressors.Xz; -using iMGcompress_core; -//using SixLabors.ImageSharp; -//using SixLabors.ImageSharp; -//using SixLabors.ImageSharp.Processing; - -namespace ComicCompressor -{ - public class Compressor - { - private long last_mem = 0; - private Logger Logger { get; set; } - - public int Quality { get; set; } = 75; - - public Compressor(Logger logger) - { - Logger = logger; - } - - public void Compress(string filename, string outputFile = null) - { - Stopwatch sw = Stopwatch.StartNew(); - outputFile = Path.ChangeExtension(outputFile ?? filename, "cbz"); - - Process proc = Process.GetCurrentProcess(); - //Logger.LogDebug($"M {proc.PrivateMemorySize64}", LogLevel.Verbose, col: Color.Green); - last_mem = proc.PrivateMemorySize64; - - //Logger.Log("Processing: " + filename, LogLevel.Verbose); - - IArchive archive = null; - //SharpCompress.Readers.ReaderOptions opt = new ReaderOptions() - //{ - - //} - - ArchiveType ArchiveType = ArchiveType.Zip; - using (Stream stream = File.OpenRead(filename)) - { - using (var reader = ReaderFactory.Open(stream)) - ArchiveType = reader.ArchiveType; - - } - - bool isSolid = false; - //if (filename.EndsWith(".cbr")) - if (ArchiveType == ArchiveType.Rar) - { - archive = RarArchive.Open(filename); - //foreach (var entry in archive.Entries) - //{ - // isSolid = isSolid || entry.IsSolid; - //} - isSolid = archive.IsSolid; - } - //else if (filename.EndsWith(".cbz")) - else if (ArchiveType == ArchiveType.Zip) - { - archive = ZipArchive.Open(filename); - } - - //Logger.Log($"Processing[{archive.Entries.Count()}]: " + filename, LogLevel.Verbose); - Logger.Log($"Processing[{archive.Entries.Count()}]: ", LogLevel.Verbose, false); - Logger.Log(filename, LogLevel.Verbose, col: Color.Green); - - double save = 0; - int num_pages = 0; - if (isSolid) - { - using (Stream stream = File.OpenRead(filename)) - { - using (var reader = ReaderFactory.Open(stream)) - { - (save, num_pages) = ProcessReader(archive, reader, outputFile); - } - } - } - else - (save, num_pages) = ProcessArchive(archive, outputFile, filename); - if (num_pages == 0) num_pages = 1; - long elapsed = sw.ElapsedMilliseconds; - Logger.Log($"\r\nFinished({save.ToString("0.##")}%) [{elapsed}][{elapsed / num_pages}]: " + filename, LogLevel.Verbose, col: Color.Green); - } - - private (double, int) ProcessReader(IArchive archive, IReader reader, string outputPath) - { - var encoder = new SimpleEncoder(); - - - bool cover = true; - double save = 0.0; - int nop = 0; - using (var output = ZipArchive.Create()) - { - var imageStreams = new List(); - - while (reader.MoveToNextEntry()) - { - if (!reader.Entry.IsDirectory) - { - //Console.WriteLine(reader.Entry.Key); - - if (reader.Entry.Key.StartsWith("__MACOSX")) - { - continue; - } - - using (var entryStream = reader.OpenEntryStream()) - { - - string entry_key_lower = reader.Entry.Key.ToLower(); - if (!entry_key_lower.EndsWith(".jpg") && - !entry_key_lower.EndsWith(".png") && - !entry_key_lower.EndsWith(".bmp") && - !entry_key_lower.EndsWith(".ppm") && - !entry_key_lower.EndsWith(".pbm") && - !entry_key_lower.EndsWith(".gif") && - !entry_key_lower.EndsWith(".jpeg")) - { - var ms = new MemoryStream(); - imageStreams.Add(ms); - entryStream.CopyTo(ms); - output.AddEntry(reader.Entry.Key, ms); - Logger.Log("o", LogLevel.Verbose, false, col: Color.PaleGreen); - continue; - } - - nop++; - - if (cover) - { - var ms = new MemoryStream(); - imageStreams.Add(ms); - entryStream.CopyTo(ms); - output.AddEntry(reader.Entry.Key, ms); - Logger.Log("C", LogLevel.Verbose, false, col: Color.Green); - cover = false; - continue; - } - - ProcessReaderEntry(reader, entryStream, imageStreams, output, encoder); - } - } - } - - reader.Dispose(); - - Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); - - output.SaveTo(outputPath, CompressionType.Deflate); - - //output.SaveTo(outputPath, CompressionType.None); - - imageStreams.ForEach(i => i.Dispose()); - } - - long output_length = new System.IO.FileInfo(outputPath).Length; - - save = 100 * (double)output_length / (double)archive.TotalSize; - - return (save, nop); - } - public int GetPlace(int value, int place) - { - return ((value % (place * 10)) - (value % place)) / place; - } - private (double, int) ProcessArchive(IArchive archive, string outputPath, string inputPath) - { - //var fileEntries = archive.Entries.Where(e => !e.IsDirectory); - var fileEntries = archive.Entries.Where(e => !e.IsDirectory).OrderBy(e => e.Key); - - var encoder = new SimpleEncoder(); - - bool cover = true; - double save = 0.0; - int nop = 0; - using (var output = ZipArchive.Create()) - { - var imageStreams = new List(); - int count = 0; - foreach (var entry in fileEntries) - { - (bool res, bool skipped) = ProcessEntry(entry, imageStreams, output, encoder, cover); - if (res) - cover = false; - count++; - if (!skipped) - nop++; - if (count % 10 == 0) - { - string digit = GetPlace(count, 10).ToString(); - Logger.Log(digit, LogLevel.Verbose, false, col: Color.Green); - } - - } - - archive.Dispose(); - - Directory.CreateDirectory(Path.GetDirectoryName(outputPath)); - -// output.SaveTo(outputPath, CompressionType.Deflate); - output.SaveTo(outputPath, CompressionType.None); - - //output.SaveTo(outputPath, CompressionType.None); - - imageStreams.ForEach(i => i.Dispose()); - - output.Dispose(); - } - - long output_length = new System.IO.FileInfo(outputPath).Length; - - save = 100 * (double)output_length / (double)archive.TotalSize; - - Program.file_stat fs = null; - Program.File_statistics.TryGetValue(inputPath, out fs); - if (fs != null) - { - fs.Uncompressed_file_len = archive.TotalSize; - fs.Compressed_file_len = output_length; - Program.File_statistics.TryAdd(inputPath, fs); - } - - - - return (save, nop); - } - - - private (bool, bool) ProcessEntry(IArchiveEntry entry, IList imageStreams, ZipArchive output, SimpleEncoder encoder, bool cover) - { - try - { - if (entry.IsEncrypted) - { - Logger.Log("X", LogLevel.Verbose, false, col: Color.Yellow); - return (false, true); - } - - - using (var stream = entry.OpenEntryStream()) - { - - if (entry.Key.StartsWith("__MACOSX")) - { - return (false, true); - } - string entry_key_lower = entry.Key.ToLower(); - bool isPPM = entry_key_lower.EndsWith(".ppm"); - bool isPBM = entry_key_lower.EndsWith(".pbm"); - - if (!entry_key_lower.EndsWith(".jpg") && - !entry_key_lower.EndsWith(".png") && - !entry_key_lower.EndsWith(".bmp") && - !entry_key_lower.EndsWith(".ppm") && - !entry_key_lower.EndsWith(".pbm") && - !entry_key_lower.EndsWith(".gif") && - !entry_key_lower.EndsWith(".jpeg")) - { - var ms = new MemoryStream(); - imageStreams.Add(ms); - stream.CopyTo(ms); - output.AddEntry(entry.Key, ms); - Logger.Log("o", LogLevel.Verbose, false, col: Color.Green); - return (false, true); - } - - if (cover && !isPPM && !isPBM) - { - var ms = new MemoryStream(); - imageStreams.Add(ms); - stream.CopyTo(ms); - output.AddEntry(entry.Key, ms); - Logger.Log("C", LogLevel.Verbose, false, col: Color.Green); - return (true, false); - } - - Bitmap bits; - - var raw_ms = new MemoryStream(); - stream.CopyTo(raw_ms); - try - { - if (isPPM) - { - Logger.Log("P", LogLevel.Verbose, false, col: Color.PaleGreen); - PixelMap ppm = new PixelMap(raw_ms); - //bits = ppm.Bitmap; - bits = ppm.BitMap; - Logger.Log("\b", LogLevel.Verbose, false, col: Color.PaleGreen); - } - else if (isPBM) - { - Logger.Log("B", LogLevel.Verbose, false, col: Color.PaleGreen); - PixelMap pbm = new PixelMap(raw_ms); - bits = pbm.BitMap; - Logger.Log("\b", LogLevel.Verbose, false, col: Color.PaleGreen); - - } - else - { - bits = new Bitmap(raw_ms); - } - } - catch (Exception e) - { - Logger.LogError("Error parsing bitmap: " + entry.Key, col: Color.Red); - Logger.LogDebug(e, LogLevel.Error, col: Color.Red); - return (false, true); - } - - if (bits.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) - { - var newBits = ChangePixelFormat(bits); - bits.Dispose(); - bits = newBits; - } - - try - { - var ms = new MemoryStream(); - encoder.Encode(bits, ms, Quality); - if (ms.Length < entry.Size || isPBM) - { - imageStreams.Add(ms); - output.AddEntry(entry.Key + ".webp", ms); - Logger.Log(".", LogLevel.Verbose, false, col: Color.PaleGreen); - - // release not used stream - raw_ms.Dispose(); - - } - else - { - imageStreams.Add(raw_ms); - - raw_ms.Seek(0, SeekOrigin.Begin); - //stream.Seek(0,SeekOrigin.Begin); - output.AddEntry(entry.Key, raw_ms); - Logger.Log("=", LogLevel.Verbose, false, col: Color.Green); - - // release not used stream - ms.Dispose(); - } - } - catch (Exception e) - { - Logger.LogError("Error encoding entry: " + entry.Key, col: Color.Red); - Logger.LogDebug(e, LogLevel.Error, col: Color.Red); - } - finally - { - bits.Dispose(); - } - } - } - catch (Exception e) - { - Logger.LogError("Error Archive Extraction: " + entry.Key, col: Color.Red); - Logger.LogDebug(e, LogLevel.Error, col: Color.Red); - return (false, true); - } - - Process proc = Process.GetCurrentProcess(); - //Logger.LogDebug($"M {proc.PrivateMemorySize64} - D {proc.PrivateMemorySize64 - last_mem}", LogLevel.Verbose); - //last_mem = proc.PrivateMemorySize64; - return (false, false); - } - - private void ProcessReaderEntry(IReader reader, EntryStream entry, IList imageStreams, ZipArchive output, SimpleEncoder encoder) - { - try - { - var stream = entry; - // using (var stream = entry.OpenEntryStream()) - { - - - Bitmap bits; - - var raw_ms = new MemoryStream(); - imageStreams.Add(raw_ms); - stream.CopyTo(raw_ms); - try - { - bits = new Bitmap(raw_ms); - } - catch (Exception e) - { - Logger.LogError("Error parsing bitmap: " + reader.Entry.Key, col: Color.Red); - Logger.LogDebug(e, LogLevel.Error, col: Color.Red); - return; - } - - if (bits.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) - { - var newBits = ChangePixelFormat(bits); - bits.Dispose(); - bits = newBits; - } - - try - { - var ms = new MemoryStream(); - encoder.Encode(bits, ms, Quality); - if (ms.Length < reader.Entry.Size) - { - imageStreams.Add(ms); - output.AddEntry(reader.Entry.Key + ".webp", ms); - Logger.Log("s", LogLevel.Verbose, false, col: Color.Green); - } - else - { - raw_ms.Seek(0, SeekOrigin.Begin); - //stream.Seek(0,SeekOrigin.Begin); - output.AddEntry(reader.Entry.Key, raw_ms); - Logger.Log("=", LogLevel.Verbose, false, col: Color.Green); - } - } - catch (Exception e) - { - Logger.LogError("Error encoding entry: " + reader.Entry.Key, col: Color.Red); - Logger.LogDebug(e, LogLevel.Error, col: Color.Red); - } - finally - { - bits.Dispose(); - } - } - } - catch (Exception e) - { - Logger.LogError("Error Archive Extraction: " + reader.Entry.Key, col: Color.Red); - Logger.LogDebug(e, LogLevel.Error, col: Color.Red); - return; - } - - return; - } - - private Bitmap ChangePixelFormat(Bitmap orig, System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format24bppRgb) - { - Bitmap clone = new Bitmap(orig.Width, orig.Height, format); - - using (Graphics gr = Graphics.FromImage(clone)) - { - gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height)); - } - - return clone; - - } - - public void CompressPdf(string filename, string outputFile = null) - { - new ImageExtractor(Logger).ExtractImagesFromFile(filename, outputFile); - } - } -} \ No newline at end of file diff --git a/Program.cs b/Program.cs index d5e9e5f..5b25661 100644 --- a/Program.cs +++ b/Program.cs @@ -22,7 +22,7 @@ using Xabe.FFmpeg; using SharpCompress.Archives; using SharpCompress.Archives.Zip; using SharpCompress.Common; -using itext.pdfimage.Extensions; +//using itext.pdfimage.Extensions; using org.omg.PortableInterceptor; using org.apache.pdfbox.util.@operator; using java.nio; @@ -40,12 +40,15 @@ using iMGcompress_core; +using iMGcompress_core; +using Compressor = iMGcompress_core.Compressor; + namespace ComicCompressor { public class Program { - // string progVersion = "Version 2.0.0 - (04/07/2024)"; + // string progVersion = "Version 2.0.0 - (04/07/2024)"; // Added copy of non image files with same name //string progVersion = "Version 2.0.1 - (12/08/2024)"; @@ -54,8 +57,13 @@ namespace ComicCompressor //string progVersion = "Version 2.0.2 - (27/08/2024)"; // Completed support for ZEBRA PRN files, generate image and base64 for prn and image. Using URI format for base64 files - string progVersion = "Version 2.1.0 - (28/08/2024)"; + //string progVersion = "Version 2.1.0 - (28/08/2024)"; + // Moved functionalities to library iMGcompress-core + //string progVersion = "Version 3.0.0 - (21/11/2024)"; + + // In core changed color of percent save to white + string progVersion = "Version 3.0.1 - (23/01/2025)"; public static int Main(string[] args) => CommandLineApplication.Execute(args); #region INPUT_FLAGS @@ -84,7 +92,6 @@ namespace ComicCompressor [Option("-c|--cpu", Description = "Run in parallel, use given number of CPUs")] public int NumOfCpu { get; } = Environment.ProcessorCount; - [Option("-d|--pdf", Description = "Process pdf only")] public bool DoPdf { get; } = false; @@ -100,18 +107,6 @@ namespace ComicCompressor #region STATISTICS - public class file_stat - { - public string Filename { get; set; } - public file_stat(string filename) { - Filename = filename; - } - public long Uncompressed_file_len { get; set; } - public long Compressed_file_len { get; set; } - - - } - static Dictionary file_statistics = new Dictionary(); public static Dictionary File_statistics { get => file_statistics; set => file_statistics = value; } @@ -169,13 +164,14 @@ namespace ComicCompressor Logger.Log(progVersion, LogLevel.Verbose, true); - var progress = new Progress( p => Logger.Log(p.ToString(), LogLevel.Verbose, true) ); - FFmpeg.SetExecutablesPath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)); - FFmpegDownloader.GetLatestVersion(FFmpegVersion.Official,progress); + //var progress = new Progress( p => Logger.Log(p.ToString(), LogLevel.Verbose, true) ); + //FFmpeg.SetExecutablesPath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)); + //FFmpegDownloader.GetLatestVersion(FFmpegVersion.Official,progress); - //FFmpeg.SetExecutablesPath (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FFmpeg")); - Logger.Log(FFmpeg.ExecutablesPath, LogLevel.Verbose, true); + //==//FFmpeg.SetExecutablesPath (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FFmpeg")); + + //Logger.Log(FFmpeg.ExecutablesPath, LogLevel.Verbose, true); IList files = new FileParser().ListAllFiles(Input, Recursive); @@ -299,7 +295,7 @@ namespace ComicCompressor { long lengthin = new System.IO.FileInfo(filename).Length; long lengthout = new System.IO.FileInfo(outputPath).Length; - Program.file_stat fs = null; + file_stat fs = null; Program.File_statistics.TryGetValue(filename, out fs); if (fs != null) { @@ -316,7 +312,7 @@ namespace ComicCompressor { long lengthin = new System.IO.FileInfo(filename).Length; long lengthout = new System.IO.FileInfo(outputPath_webp).Length; - Program.file_stat fs = null; + file_stat fs = null; Program.File_statistics.TryGetValue(filename, out fs); if (fs != null) { @@ -336,7 +332,7 @@ namespace ComicCompressor long lengthin = new System.IO.FileInfo(filename).Length; long lengthout = new System.IO.FileInfo(outputPath).Length; - Program.file_stat fs = null; + file_stat fs = null; Program.File_statistics.TryGetValue(filename, out fs); if (fs != null) { @@ -422,7 +418,7 @@ namespace ComicCompressor long lengthin = new System.IO.FileInfo(filename).Length; long lengthout = new System.IO.FileInfo(outputPath).Length; - Program.file_stat fs = null; + file_stat fs = null; Program.File_statistics.TryGetValue(filename, out fs); if (fs != null) { @@ -464,7 +460,7 @@ namespace ComicCompressor try { - compressor.Compress(filename, outputPath); + compressor.Compress(filename, outputPath, file_statistics); } catch (Exception e) { diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index 65d0ded..1a72bc7 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -26,11 +26,16 @@ }, "DEV_UFF": { "commandName": "Project", - "commandLineArgs": "-i \"E:\\SynologyDrive\\SynologyDrive\\A\\_PRINT_TEMPLATES_\\DEV_IN\" -o \"E:\\SynologyDrive\\SynologyDrive\\A\\_PRINT_TEMPLATES_\\DEV_OUT\" -r -s -j -a -n" + "commandLineArgs": "-i \"E:\\SynologyDrive\\SynologyDrive\\A\\_PRINT_TEMPLATES_\\DEV_IN\" -o \"E:\\SynologyDrive\\SynologyDrive\\A\\_PRINT_TEMPLATES_\\DEV_OUT\" -r -s -j -a -n", + "nativeDebugging": true }, "DEV_HOME": { "commandName": "Project", "commandLineArgs": "-i \"D:\\SynologyDrive\\A\\_PRINT_TEMPLATES_\\DEV_IN\" -o \"D:\\SynologyDrive\\A\\_PRINT_TEMPLATES_\\DEV_OUT\" -r -s -j -a -n" + }, + "ComicCompress CBx": { + "commandName": "Project", + "commandLineArgs": "-i _IMG_ -o _IMG_PDF_ -r -s -d" } } } \ No newline at end of file