experimental/xps_to_png: fix DPI, usage message
authorHal Canary <halcanary@google.com>
Mon, 17 Oct 2016 15:52:40 +0000 (11:52 -0400)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Mon, 17 Oct 2016 15:58:05 +0000 (15:58 +0000)
NOTRY=true
Change-Id: I4d4c46b5889ab8e8b8f54e8c631b3932912051dd
Reviewed-on: https://skia-review.googlesource.com/3534
Reviewed-by: Hal Canary <halcanary@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>

experimental/xps_to_png/.gitignore [new file with mode: 0644]
experimental/xps_to_png/compile_xps_to_png.bat
experimental/xps_to_png/xps_to_png.cs

diff --git a/experimental/xps_to_png/.gitignore b/experimental/xps_to_png/.gitignore
new file mode 100644 (file)
index 0000000..335ad17
--- /dev/null
@@ -0,0 +1 @@
+xps_to_png.exe
index 4c34293..b4140d4 100644 (file)
@@ -1,14 +1,14 @@
-@rem Copyright 2016 Google Inc.\r
-@rem\r
-@rem Use of this source code is governed by a BSD-style license that can be\r
-@rem found in the LICENSE file.\r
-\r
-"C:\PROGRA~2\MSBUILD\14.0\BIN\AMD64\CSC.EXE" ^\r
-/lib:"\PROGRA~2\REFERE~1\MICROS~1\FRAMEW~1\NETFRA~1\V4.5.2" ^\r
-/reference:"ReachFramework.dll" ^\r
-/reference:"WindowsBase.dll" ^\r
-/reference:"PresentationCore.dll" ^\r
-/reference:"PresentationFramework.dll" ^\r
-"%~dp0%xps_to_png.cs"\r
-\r
-\r
+@rem Copyright 2016 Google Inc.
+@rem
+@rem Use of this source code is governed by a BSD-style license that can be
+@rem found in the LICENSE file.
+
+@set CSC="C:\Program Files (x86)\MSBuild\14.0\Bin\amd64\csc.exe"
+@set LIB="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2"
+
+%CSC% /lib:%LIB% ^
+/reference:"PresentationCore.dll" ^
+/reference:"PresentationFramework.dll" ^
+/reference:"ReachFramework.dll" ^
+/reference:"WindowsBase.dll" ^
+"%~dp0%xps_to_png.cs"
index 951d4d7..34b9132 100644 (file)
@@ -1,77 +1,94 @@
-/*\r
- * Copyright 2016 Google Inc.\r
- *\r
- * Use of this source code is governed by a BSD-style license that can be\r
- * found in the LICENSE file.\r
- */\r
-\r
-/*\r
-Compile with:\r
-\r
-    "...../csc" \\r
-        /lib:"....." \\r
-        /reference:"ReachFramework.dll" \\r
-        /reference:"WindowsBase.dll" \\r
-        /reference:"PresentationCore.dll" \\r
-        /reference:"PresentationFramework.dll" \\r
-        xps_to_png.cs\r
-\r
-*/\r
-// logic inspired by this example: https://goo.gl/nCxrjQ\r
-class Program {\r
-    static void convert(string path, string out_path) {\r
-        System.Windows.Xps.Packaging.XpsDocument xpsDoc =\r
-            new System.Windows.Xps.Packaging.XpsDocument(path, System.IO.FileAccess.Read);\r
-        if (xpsDoc == null) {\r
-            throw new System.Exception("XpsDocumentfailed");\r
-        }\r
-        System.Windows.Documents.FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();\r
-        if (docSeq == null) {\r
-            throw new System.Exception("GetFixedDocumentSequence failed");\r
-        }\r
-        System.Windows.Documents.DocumentReferenceCollection drc = docSeq.References;\r
-        int index = 0;\r
-        foreach (System.Windows.Documents.DocumentReference dr in drc) {\r
-            System.Windows.Documents.FixedDocument dp = dr.GetDocument(false);\r
-            foreach (System.Windows.Documents.PageContent pc in dp.Pages) {\r
-                System.Windows.Documents.FixedPage fixedPage = pc.GetPageRoot(false);\r
-                double width = fixedPage.Width;\r
-                double height = fixedPage.Height;\r
-                System.Windows.Size sz = new System.Windows.Size(width, height);\r
-                fixedPage.Measure(sz);\r
-                fixedPage.Arrange(new System.Windows.Rect(new System.Windows.Point(), sz));\r
-                fixedPage.UpdateLayout();\r
-                System.Windows.Media.Imaging.BitmapImage bitmap = new System.Windows.Media.Imaging.BitmapImage();\r
-                System.Windows.Media.Imaging.RenderTargetBitmap renderTarget =\r
-                    new System.Windows.Media.Imaging.RenderTargetBitmap((int)width, (int)height, 96, 96,\r
-                                                                        System.Windows.Media.PixelFormats.Default);\r
-                renderTarget.Render(fixedPage);\r
-                System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();\r
-                encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(renderTarget));\r
-                System.IO.FileStream pageOutStream = new System.IO.FileStream(\r
-                    string.Format("{0}_{1}.png", out_path, index),\r
-                    System.IO.FileMode.Create, System.IO.FileAccess.Write);\r
-                encoder.Save(pageOutStream);\r
-                pageOutStream.Close();\r
-                ++index;\r
-            }\r
-        }\r
-    }\r
-        // Executes convert, catching thrown exceptions, and printing them to stdout, and exiting immediately\r
-    static void try_convert(string path, string out_path) {\r
-        try {\r
-            convert(path, out_path);\r
-        } catch (System.Exception e) {\r
-            System.Console.WriteLine(e);\r
-            System.Environment.Exit(1);\r
-        }\r
-    }\r
-        // For each command line argument, convert xps to sequence of pngs\r
-    static void Main(string[] args) {\r
-        foreach (string arg in args) {\r
-            System.Threading.Thread t = new System.Threading.Thread(() => try_convert(arg, arg));\r
-            t.SetApartmentState(System.Threading.ApartmentState.STA);\r
-            t.Start();\r
-        }\r
-    }\r
-}\r
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/*
+Compile with:
+
+    "...../csc" \
+        /lib:"....." \
+        /reference:"ReachFramework.dll" \
+        /reference:"WindowsBase.dll" \
+        /reference:"PresentationCore.dll" \
+        /reference:"PresentationFramework.dll" \
+        xps_to_png.cs
+
+*/
+// logic inspired by this example: https://goo.gl/nCxrjQ
+class Program {
+    static int ceil(double x) { return (int)System.Math.Ceiling(x); }
+    static void convert(double dpi, string path, string out_path) {
+        double scale = dpi / 96.0;
+        System.Windows.Xps.Packaging.XpsDocument xpsDoc =
+                new System.Windows.Xps.Packaging.XpsDocument(
+                        path, System.IO.FileAccess.Read);
+        if (xpsDoc == null) {
+            throw new System.Exception("XpsDocumentfailed");
+        }
+        System.Windows.Documents.FixedDocumentSequence docSeq =
+                xpsDoc.GetFixedDocumentSequence();
+        if (docSeq == null) {
+            throw new System.Exception("GetFixedDocumentSequence failed");
+        }
+        System.Windows.Documents.DocumentReferenceCollection drc = docSeq.References;
+        int index = 0;
+        foreach (System.Windows.Documents.DocumentReference dr in drc) {
+            System.Windows.Documents.FixedDocument dp = dr.GetDocument(false);
+            foreach (System.Windows.Documents.PageContent pc in dp.Pages) {
+                System.Windows.Documents.FixedPage fixedPage = pc.GetPageRoot(false);
+                double width = fixedPage.Width;
+                double height = fixedPage.Height;
+                System.Windows.Size sz = new System.Windows.Size(width, height);
+                fixedPage.Measure(sz);
+                fixedPage.Arrange(
+                        new System.Windows.Rect(new System.Windows.Point(), sz));
+                fixedPage.UpdateLayout();
+                System.Windows.Media.Imaging.BitmapImage bitmap =
+                        new System.Windows.Media.Imaging.BitmapImage();
+                System.Windows.Media.Imaging.RenderTargetBitmap renderTarget =
+                        new System.Windows.Media.Imaging.RenderTargetBitmap(
+                            ceil(scale * width), ceil(scale * height), dpi, dpi,
+                            System.Windows.Media.PixelFormats.Default);
+                renderTarget.Render(fixedPage);
+                System.Windows.Media.Imaging.BitmapEncoder encoder =
+                    new System.Windows.Media.Imaging.PngBitmapEncoder();
+                encoder.Frames.Add(
+                        System.Windows.Media.Imaging.BitmapFrame.Create(renderTarget));
+                string filename = string.Format("{0}_{1}.png", out_path, index);
+                System.IO.FileStream pageOutStream = new System.IO.FileStream(
+                    filename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
+                encoder.Save(pageOutStream);
+                pageOutStream.Close();
+                System.Console.WriteLine(filename);
+                ++index;
+            }
+        }
+    }
+    // Executes convert, catching thrown exceptions, and printing them
+    // to stdout, and exiting immediately.
+    static void try_convert(double dpi, string path, string out_path) {
+        try {
+            convert(dpi, path, out_path);
+        } catch (System.Exception e) {
+            System.Console.WriteLine(e);
+            System.Environment.Exit(1);
+        }
+    }
+    // For each command line argument, convert xps to sequence of pngs.
+    static void Main(string[] args) {
+        const double dpi = 72.0;
+        if (args.Length == 0) {
+            System.Console.WriteLine("usage:\n\txps_to_png [XPS_FILES]\n\n");
+            System.Environment.Exit(1);
+        }
+        foreach (string arg in args) {
+            System.Threading.Thread t = new System.Threading.Thread(
+                    () => try_convert(dpi, arg, arg));
+            t.SetApartmentState(System.Threading.ApartmentState.STA);
+            t.Start();
+        }
+    }
+}