From c693c96100b861d3d78722af4cc1c4bef6a18918 Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Fri, 17 Nov 2023 04:31:38 +0100 Subject: [PATCH] Converter and direct BASE64 files --- .gitignore | 8 +- PackageSources/converter/Program.cs | 113 ++++++++++++++++++ PackageSources/converter/converter.csproj | 26 ++++ PackageSources/converter/converter.sln | 25 ++++ .../js-bundle/src/pages/PDFPage/PDFPage.tsx | 4 +- PackageSources/module/FileSystem.hpp | 10 +- 6 files changed, 178 insertions(+), 8 deletions(-) create mode 100644 PackageSources/converter/Program.cs create mode 100644 PackageSources/converter/converter.csproj create mode 100644 PackageSources/converter/converter.sln diff --git a/.gitignore b/.gitignore index 18ef266..b452c0e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,10 @@ PackageSources/js-bundle/.rollup.cache PackageSources/js-bundle/node_modules PackageSources/html_ui PackageSources/module/.vs -PackageSources/module/MSFS \ No newline at end of file +PackageSources/module/MSFS +PackageSources/converter/bin +PackageSources/converter/obj +PackageSources/converter/.vs + +PackageSources/converter/*.user +PackageSources/converter/Properties/PublishProfiles diff --git a/PackageSources/converter/Program.cs b/PackageSources/converter/Program.cs new file mode 100644 index 0000000..79e0761 --- /dev/null +++ b/PackageSources/converter/Program.cs @@ -0,0 +1,113 @@ +using Docnet.Core.Models; +using Docnet.Core.Readers; +using Docnet.Core; +using System.Drawing; +using System.Drawing.Imaging; +using System.Runtime.InteropServices; +using Docnet.Core.Exceptions; + +#region Main Code + +if (args.Length == 0) +{ + Console.WriteLine("No PDF specified"); + return 1; +} + +Console.WriteLine($"Conversion for {args[0]}"); +Console.WriteLine("Using configuration"); +Console.WriteLine("\tA4 at 96dpi"); +Console.WriteLine(); +return ReadPDF(args[0]); + +#endregion + +#region Functions + +int ReadPDF(string path) +{ + string outPath = ""; + try + { + outPath = Path.GetFileNameWithoutExtension(path); + Directory.CreateDirectory(outPath); + } catch (Exception e) + { + Console.Error.WriteLine("Error creating file directory"); + Console.Error.WriteLine(e.Message); + Console.ReadKey(); + return 2; + } + + try + { + using (IDocReader docReader = DocLib.Instance.GetDocReader(path, new PageDimensions(794, 1122))) + { + int pages = docReader.GetPageCount(); + Console.WriteLine($"Converting {pages} pages"); + + for (int i = 0; i < pages; i++) + { + using (IPageReader pageReader = docReader.GetPageReader(i)) + { + byte[] rawBytes = pageReader.GetImage(); + int width = pageReader.GetPageWidth(); + int height = pageReader.GetPageHeight(); + + using (Bitmap doc = new Bitmap(width, height, PixelFormat.Format32bppArgb)) + using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb)) + { + AddBytes(bmp, rawBytes); + + Graphics g = Graphics.FromImage(doc); + g.FillRegion(Brushes.White, new Region(new Rectangle(0, 0, width, height))); + g.DrawImage(bmp, new Point(0, 0)); + g.Save(); + + if (i == 0) + { + Bitmap thumb = new Bitmap(doc, new Size(doc.Width / 10, doc.Height / 10)); + using (MemoryStream stream = new MemoryStream()) + { + //saving and exporting + thumb.Save(stream, ImageFormat.Jpeg); + Console.WriteLine($"Ouputing tumbnail"); + File.WriteAllText($"{outPath}\\thumb.bjpg", Convert.ToBase64String(stream.ToArray())); + }; + } + + using (MemoryStream stream = new MemoryStream()) + { + //saving and exporting + doc.Save(stream, ImageFormat.Jpeg); + Console.WriteLine($"Ouputing page {i + 1}"); + File.WriteAllText($"{outPath}\\{i + 1}.bjpg", Convert.ToBase64String(stream.ToArray())); + }; + }; + }; + }; + } + } + catch(DocnetLoadDocumentException) + { + Console.Error.WriteLine("File is not a PDF"); + Console.ReadKey(); + return 3; + } + + Console.ReadKey(); + return 0; +} + +void AddBytes(Bitmap bmp, byte[] rawBytes) +{ + Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); + + BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat); + nint pNative = bmpData.Scan0; + + Marshal.Copy(rawBytes, 0, pNative, rawBytes.Length); + bmp.UnlockBits(bmpData); +} + +#endregion \ No newline at end of file diff --git a/PackageSources/converter/converter.csproj b/PackageSources/converter/converter.csproj new file mode 100644 index 0000000..22b7dfb --- /dev/null +++ b/PackageSources/converter/converter.csproj @@ -0,0 +1,26 @@ + + + + Exe + net8.0-windows + enable + enable + False + full + True + + + + 9999 + + + + 9999 + + + + + + + + diff --git a/PackageSources/converter/converter.sln b/PackageSources/converter/converter.sln new file mode 100644 index 0000000..0ab8be3 --- /dev/null +++ b/PackageSources/converter/converter.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34309.116 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "converter", "converter.csproj", "{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D95A0B4E-6516-4CBE-84E0-5FF1068CEF60} + EndGlobalSection +EndGlobal diff --git a/PackageSources/js-bundle/src/pages/PDFPage/PDFPage.tsx b/PackageSources/js-bundle/src/pages/PDFPage/PDFPage.tsx index fe6ef87..cc21362 100644 --- a/PackageSources/js-bundle/src/pages/PDFPage/PDFPage.tsx +++ b/PackageSources/js-bundle/src/pages/PDFPage/PDFPage.tsx @@ -17,7 +17,7 @@ const PDFPage: FC = () => { const [pageJump, setPageJump] = useState('1'); useEffect(() => { - load(`${path}/${entry.name}/${currentPage}.png`); + load(`${path}/${entry.name}/${currentPage}.bjpg`); }, [currentPage]); return ( @@ -77,7 +77,7 @@ const PDFPage: FC = () => { ) : ( - + )} diff --git a/PackageSources/module/FileSystem.hpp b/PackageSources/module/FileSystem.hpp index 58fcc15..52b9341 100644 --- a/PackageSources/module/FileSystem.hpp +++ b/PackageSources/module/FileSystem.hpp @@ -14,11 +14,11 @@ namespace khofmann { /// - /// Read a file and return BASE64 encoded file contents + /// Read a file and return file contents /// - /// Path of File + /// Path of BASE64 encoded File /// Allocator - /// BASE64 encoded file contents + /// File contents static rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc) { FILE* file = fopen(path, "r"); @@ -31,7 +31,7 @@ namespace khofmann fread(string, fsize, 1, file); fclose(file); - return rapidjson::Value(macaron::Base64::Encode(string, fsize).c_str(), alloc); + return rapidjson::Value(string, alloc); } return rapidjson::Value("", alloc); @@ -63,7 +63,7 @@ namespace khofmann std::string dirPath(path); dirPath += dir->d_name; std::string thumb(dirPath); - thumb += "/thumb.png"; + thumb += "/thumb.bjpg"; FILE* check = fopen(thumb.c_str(), "r"); if (check) {