aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/xps_to_png
diff options
context:
space:
mode:
authorGravatar Hal Canary <halcanary@google.com>2016-10-13 15:30:11 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2016-10-14 13:27:55 +0000
commit2eaef920ebb241d718e0c1facb966eefb1f2aefb (patch)
tree7604d5beaadcecfbb305712bc9981d97e519be29 /experimental/xps_to_png
parent703cf5aa20aaf01476f99299ca70a7e8b7aab4c5 (diff)
experimental: xps_to_png program (windows only)
BUG=skia:409 NOTRY=true Change-Id: Ib0df603535de71b0a324808c17e47691435df666 Reviewed-on: https://skia-review.googlesource.com/3342 Reviewed-by: Hal Canary <halcanary@google.com> Commit-Queue: Hal Canary <halcanary@google.com>
Diffstat (limited to 'experimental/xps_to_png')
-rw-r--r--experimental/xps_to_png/compile_xps_to_png.bat14
-rw-r--r--experimental/xps_to_png/xps_to_png.cs77
2 files changed, 91 insertions, 0 deletions
diff --git a/experimental/xps_to_png/compile_xps_to_png.bat b/experimental/xps_to_png/compile_xps_to_png.bat
new file mode 100644
index 0000000000..4c34293f35
--- /dev/null
+++ b/experimental/xps_to_png/compile_xps_to_png.bat
@@ -0,0 +1,14 @@
+@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.
+
+"C:\PROGRA~2\MSBUILD\14.0\BIN\AMD64\CSC.EXE" ^
+/lib:"\PROGRA~2\REFERE~1\MICROS~1\FRAMEW~1\NETFRA~1\V4.5.2" ^
+/reference:"ReachFramework.dll" ^
+/reference:"WindowsBase.dll" ^
+/reference:"PresentationCore.dll" ^
+/reference:"PresentationFramework.dll" ^
+"%~dp0%xps_to_png.cs"
+
+
diff --git a/experimental/xps_to_png/xps_to_png.cs b/experimental/xps_to_png/xps_to_png.cs
new file mode 100644
index 0000000000..951d4d79a2
--- /dev/null
+++ b/experimental/xps_to_png/xps_to_png.cs
@@ -0,0 +1,77 @@
+/*
+ * 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 void convert(string path, string out_path) {
+ 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((int)width, (int)height, 96, 96,
+ 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));
+ System.IO.FileStream pageOutStream = new System.IO.FileStream(
+ string.Format("{0}_{1}.png", out_path, index),
+ System.IO.FileMode.Create, System.IO.FileAccess.Write);
+ encoder.Save(pageOutStream);
+ pageOutStream.Close();
+ ++index;
+ }
+ }
+ }
+ // Executes convert, catching thrown exceptions, and printing them to stdout, and exiting immediately
+ static void try_convert(string path, string out_path) {
+ try {
+ convert(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) {
+ foreach (string arg in args) {
+ System.Threading.Thread t = new System.Threading.Thread(() => try_convert(arg, arg));
+ t.SetApartmentState(System.Threading.ApartmentState.STA);
+ t.Start();
+ }
+ }
+}