/*
 * Copyright (c) 2015 Raytheon BBN Technologies Corp. All rights reserved.
 *
 * Derived from code with the following license:
 *
 *  ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one or more
 *    contributor license agreements.  See the NOTICE file distributed with
 *    this work for additional information regarding copyright ownership.
 *    The ASF licenses this file to You under the Apache License, Version 2.0
 *    (the "License"); you may not use this file except in compliance with
 *    the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 * ====================================================================
 */

package org.apache.poi.xdgf.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.poi.xdgf.geom.Dimension2dDouble;
import org.apache.poi.xdgf.usermodel.XDGFPage;
import org.apache.poi.xdgf.usermodel.XmlVisioDocument;
import org.apache.poi.xdgf.usermodel.shape.ShapeRenderer;

public class VsdxToPng {

	
	public static void renderToPng(XDGFPage page, String outFilename, double scale, ShapeRenderer renderer) throws IOException {
		renderToPng(page, new File(outFilename), scale, renderer);
	}
	
	public static void renderToPngDir(XDGFPage page, File outDir, double scale, ShapeRenderer renderer) throws IOException {
		
		File pageFile = new File(outDir, "page" + page.getPageNumber() + "-" + Util.sanitizeFilename(page.getName()) + ".png");
		System.out.println("** Writing image to " + pageFile);
		
		renderToPng(page, pageFile, scale, renderer);
		
	}
	
	public static void renderToPng(XDGFPage page, File outFile, double scale, ShapeRenderer renderer) throws IOException {
		
		Dimension2dDouble sz = page.getPageSize();
		System.out.println(sz);
		int width = (int)(scale * sz.getWidth());
		int height = (int)(scale * sz.getHeight());
		
		BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        final Graphics2D graphics = img.createGraphics();

        // default rendering options
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

        graphics.setColor(Color.black);
        graphics.setBackground(Color.white);
        graphics.clearRect(0, 0, width, height);
        
        // Visio's coordinate system is flipped, so flip the image vertically
        graphics.translate(0,  img.getHeight());
        graphics.scale(scale, -scale);
        
        // toplevel shapes only
        renderer.setGraphics(graphics);
        page.getContent().visitShapes(renderer);
        
        graphics.dispose();
        
        FileOutputStream out = new FileOutputStream(outFile);
        ImageIO.write(img, "png", out);
        out.close();
	}
	
	public static void renderToPng(XmlVisioDocument document, String outDirname, double scale, ShapeRenderer renderer) throws IOException {
		
		File outDir = new File(outDirname);
		
		for (XDGFPage page: document.getPages()) {
			renderToPngDir(page, outDir, scale, renderer);
		}
	}
	
	public static void main(String [] args) throws Exception {
		if (args.length != 2) {
			System.err.println("Usage: in.vsdx outdir");
			System.exit(1);
		}
		
		String inFilename = args[0];
		String pngDir = args[1];
		
		XmlVisioDocument doc = new XmlVisioDocument(new FileInputStream(inFilename));
		renderToPng(doc, pngDir, 2000/11.0, new ShapeRenderer());
	}
}

@@ -1225,16 +1225,46 @@
 gHints;%0A
+import java.awt.geom.Point2D;%0A
 import j
@@ -1591,16 +1591,82 @@
 cument;%0A
+import org.apache.poi.xdgf.usermodel.shape.ShapeDebuggerRenderer;%0A
 import o
@@ -2473,31 +2473,8 @@
 ;%0A%09%09
-System.out.println(sz);
 %0A%09%09i
@@ -4158,10 +4158,9 @@
 gth 
-!=
+%3E
  2) 
@@ -4190,16 +4190,26 @@
 (%22Usage:
+ %5B--debug%5D
  in.vsdx
@@ -4245,16 +4245,67 @@
 %0A%09%09%7D%0A%09%09%0A
+%09%09ShapeRenderer renderer = new ShapeRenderer();%0A%09%09%0A
 %09%09String
@@ -4357,16 +4357,147 @@
 %5B1%5D;%0A%09%09%0A
+%09%09if (args%5B0%5D.equals(%22--debug%22)) %7B%0A%09%09%09inFilename = args%5B1%5D;%0A%09%09%09pngDir = args%5B2%5D;%0A%09%09%09renderer = new ShapeDebuggerRenderer();%0A%09%09%7D%0A%09%09%0A
 %09%09XmlVis
@@ -4606,35 +4606,24 @@
 0/11.0, 
-new ShapeR
+r
 enderer
-()
 );%0A%09%7D%0A%7D%0A

Add --debug option to vsdxtopng