raimund@251: /*
raimund@251:  * Copyright (c) 2011 by Intevation GmbH, Germany <info@intevation.de>
raimund@251:  *
raimund@251:  * This file is part of MXD2map.
raimund@251:  *
raimund@251:  * This program is free software under the LGPL (>=v2.1)
raimund@251:  * Read the file LICENCE.txt coming with the software for details
raimund@251:  * or visit http://www.gnu.org/licenses/ if it does not exist.
raimund@251:  *
raimund@251:  * MXD2map has been developed on behalf of the
raimund@251:  * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg
raimund@251:  * by Intevation GmbH.
raimund@251:  *
raimund@251:  * Authors:
raimund@251:  * Raimund Renkert <raimund.renkert@intevation.de>
raimund@251:  * Bjoern Schilberg <bjoern.schilberg@intevation.de>
raimund@251:  * Stephan Holl <stephan.holl@intevation.de>
raimund@251:  */
raimund@251: 
raimund@251: package de.intevation.mxd.reader;
raimund@251: 
raimund@251: import java.awt.Color;
raimund@251: import java.awt.Image;
raimund@251: 
raimund@251: import org.apache.log4j.Logger;
raimund@251: 
raimund@251: import com.esri.arcgis.display.ISymbol;
raimund@251: import com.esri.arcgis.display.ILineSymbol;
raimund@251: import com.esri.arcgis.display.IFillSymbol;
raimund@251: import com.esri.arcgis.display.PictureFillSymbol;
raimund@251: import com.esri.arcgis.support.ms.stdole.Picture;
raimund@251: import com.esri.arcgis.display.RgbColor;
raimund@251: 
raimund@251: import org.w3c.dom.Element;
raimund@251: import java.io.IOException;
raimund@251: import java.awt.image.BufferedImage;
raimund@251: import javax.imageio.ImageIO;
raimund@251: import java.io.ByteArrayOutputStream;
raimund@251: 
raimund@251: import org.apache.commons.codec.binary.Base64;
raimund@251: /**
raimund@251:  * Reads picture fill symbol information.
raimund@251:  *
raimund@251:  * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
raimund@251:  */
raimund@251: public class PictureFillSymbolReader
raimund@251: extends AbstractSymbolReader {
raimund@251: 
raimund@251:     /**
raimund@251:      * The logger.
raimund@251:      */
raimund@251:     private static final Logger logger =
raimund@251:         Logger.getLogger(PictureFillSymbolReader.class);
raimund@251: 
raimund@251:     /**
raimund@251:      * Private member.
raimund@251:      */
raimund@251:     private PictureFillSymbol symbol;
raimund@251: 
raimund@251:     public PictureFillSymbolReader(ISymbol symbol)
raimund@251:     throws Exception {
raimund@251:         logger.debug("contructor()");
raimund@251:         if(symbol instanceof PictureFillSymbol) {
raimund@251:             this.symbol = (PictureFillSymbol)symbol;
raimund@251:         }
raimund@251:         else {
raimund@251:             throw new Exception("Not a PictureFillSymbol!");
raimund@251:         }
raimund@251:     }
raimund@251: 
raimund@251:     public PictureFillSymbolReader(IFillSymbol symbol)
raimund@251:     throws Exception {
raimund@251:         logger.debug("contructor()");
raimund@251:         if(symbol instanceof PictureFillSymbol) {
raimund@251:             this.symbol = (PictureFillSymbol)symbol;
raimund@251:         }
raimund@251:         else {
raimund@251:             throw new Exception("Not a PictureFillSymbol!");
raimund@251:         }
raimund@251:     }
raimund@251: 
raimund@251:     /**
raimund@251:      * Reads the symbol attributes.
raimund@251:      *
raimund@251:      * @return The XML node.
raimund@251:      */
raimund@251:     public Element read() {
raimund@251:         logger.debug("read()");
raimund@251:         Element symbolElement = util.addSymbol(parent);
raimund@251: 
raimund@251:         try {
raimund@251:             symbolElement.setAttribute("name", symbol.getNameString());
raimund@251:         }
raimund@251:         catch(IOException ioe) {
raimund@251:             logger.warn("Could not read name. Setting name to \"default\"");
raimund@251:             symbolElement.setAttribute("name", "default");
raimund@251:         }
raimund@251: 
raimund@251:         try {
raimund@251:             symbolElement.setAttribute(
raimund@251:                 "x_scale",
raimund@251:                 String.valueOf(symbol.getXScale()));
raimund@251:         }
raimund@251:         catch(IOException ioe) {
raimund@251:             logger.warn("Could not read x-scale.");
raimund@251:         }
raimund@251: 
raimund@251:         try {
raimund@251:             symbolElement.setAttribute(
raimund@251:                 "y_scale",
raimund@251:                 String.valueOf(symbol.getYScale()));
raimund@251:         }
raimund@251:         catch(IOException ioe) {
raimund@251:             logger.warn("Could not read y-scale.");
raimund@251:         }
raimund@251: 
raimund@251:         try {
raimund@251:             symbolElement.setAttribute(
raimund@251:                 "xseparation",
raimund@251:                 String.valueOf(symbol.getXSeparation()));
raimund@251:         }
raimund@251:         catch(IOException ioe) {
raimund@251:             logger.warn("Could not read x-separation.");
raimund@251:         }
raimund@251: 
raimund@251:         try {
raimund@251:             symbolElement.setAttribute(
raimund@251:                 "yseparation",
raimund@251:                 String.valueOf(symbol.getYSeparation()));
raimund@251:         }
raimund@251:         catch(IOException ioe) {
raimund@251:             logger.warn(
raimund@251:                 "Could not read y-separation.");
raimund@251:         }
raimund@251: 
raimund@251: 	try {
raimund@251:             ILineSymbol ls = symbol.getOutline();
raimund@251:             LineSymbolReader lsr = new LineSymbolReader();
raimund@251:             if(lsr.canRead(ls)) {
raimund@251:                 lsr.setSymbol(ls);
raimund@251:                 lsr.setUtil(util);
raimund@251:                 lsr.setParent(symbolElement);
raimund@251:                 lsr.read();
raimund@251:             }
raimund@251:             else {
raimund@251:                 logger.debug("The type of " + ls.getClass().toString() +
raimund@251:                              " is not implemented!");
raimund@251:             }
raimund@251:         }
raimund@251:         catch(Exception e) {
raimund@251:             logger.warn("Could not read outline.");
raimund@251:         }
raimund@251: 
raimund@251:         //Read the picture and convert to base64.
raimund@251:         try {
raimund@251:             Picture p = symbol.getPicture();
raimund@251:             Image i = p.toImage();
raimund@251:             if(i instanceof BufferedImage) {
raimund@251:                 BufferedImage bi = (BufferedImage)i;
raimund@251:                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
raimund@251:                 //Get byte array from image.
raimund@251:                 ImageIO.write(bi, "BMP", baos);
raimund@251:                 Base64 encoder = new Base64();
raimund@251:                 //encode in a base64 string
raimund@251:                 String pict = encoder.encodeBase64String(baos.toByteArray());
raimund@251:                 symbolElement.setAttribute("picture", pict);
raimund@251: 
raimund@251:                 //Get transparent color.
raimund@251:                 RgbColor c = new RgbColor();
raimund@251:                 c.setRGB(symbol.getBitmapTransparencyColor().getRGB());
raimund@251:                 Color transp = new Color (
raimund@251:                         c.getRed(),
raimund@251:                         c.getGreen(),
raimund@251:                         c.getBlue());
raimund@251:                 symbolElement.setAttribute(
raimund@251:                     "transparent_color",
raimund@251:                     String.valueOf(transp.getRGB()));
raimund@251:             }
raimund@251:             else {
raimund@251:                 logger.warn("Could not read image symbol.");
raimund@251:                 return null;
raimund@251:             }
raimund@251:         }
raimund@251:         catch(IOException ioe) {
raimund@251:             logger.warn("Could not read picture.");
raimund@251:         }
raimund@251:         symbolElement.setAttribute("type", "fill");
raimund@251:         symbolElement.setAttribute("style", "picture");
raimund@251: 
raimund@251:         return symbolElement;
raimund@251:     }
raimund@251: }
raimund@251: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :