diff options
author | Simon Rettberg | 2014-06-27 21:22:27 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-06-27 21:22:27 +0200 |
commit | e323c3767bc6351549bfed6cec907006ea42d1ef (patch) | |
tree | 8e14c423159a1a62bf54e5458e41b4cefb9b8c92 | |
parent | Better error handling for ldadp launching (diff) | |
download | tmlite-bwlp-e323c3767bc6351549bfed6cec907006ea42d1ef.tar.gz tmlite-bwlp-e323c3767bc6351549bfed6cec907006ea42d1ef.tar.xz tmlite-bwlp-e323c3767bc6351549bfed6cec907006ea42d1ef.zip |
Branding module generator task
-rw-r--r-- | pom.xml | 5 | ||||
-rw-r--r-- | src/main/java/org/openslx/satserver/util/Ppm.java | 52 | ||||
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/BrandingGenerator.java | 124 |
3 files changed, 181 insertions, 0 deletions
@@ -74,5 +74,10 @@ <version>1.8</version> <scope>compile</scope> </dependency> + <dependency> + <groupId>com.kitfox.svg</groupId> + <artifactId>svg-salamander</artifactId> + <version>1.0</version> + </dependency> </dependencies> </project> diff --git a/src/main/java/org/openslx/satserver/util/Ppm.java b/src/main/java/org/openslx/satserver/util/Ppm.java new file mode 100644 index 0000000..29491d7 --- /dev/null +++ b/src/main/java/org/openslx/satserver/util/Ppm.java @@ -0,0 +1,52 @@ +package org.openslx.satserver.util; + +import java.awt.Color; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +public class Ppm +{ + + /** + * Save image as ppm file. + * + * @param inImage BufferedImage to save + * @param fName file name to save as + * @param white + * @throws IOException + */ + public static boolean write( BufferedImage inImage, String fName, Color transparent ) throws IOException + { + FileOutputStream bw = null; + final int tr = transparent.getRed(); + final int tg = transparent.getGreen(); + final int tb = transparent.getBlue(); + try { + File theFile = new File( fName ); + bw = new FileOutputStream( theFile ); + bw.write( "P6\n".getBytes( StandardCharsets.UTF_8 ) ); + bw.write( ( inImage.getWidth() + " " + inImage.getHeight() + " 255\n" ).getBytes( StandardCharsets.UTF_8 ) ); + + for ( int y = 0; y < inImage.getHeight(); ++y ) { + for ( int x = 0; x < inImage.getWidth(); ++x ) { + int color = inImage.getRGB( x, y ); + int alpha = ( color >>> 24 ) & 0xFF; + int red = ( color >>> 16 ) & 0xFF; + int green = ( color >>> 8 ) & 0xFF; + int blue = ( color ) & 0xFF; + bw.write( ( ( red * alpha ) + ( tr * ( 255 - alpha ) ) ) / 255 ); + bw.write( ( ( green * alpha ) + ( tg * ( 255 - alpha ) ) ) / 255 ); + bw.write( ( ( blue * alpha ) + ( tb * ( 255 - alpha ) ) ) / 255 ); + } + } + bw.close(); + } finally { + Util.multiClose( bw ); + } + return true; + } + +} diff --git a/src/main/java/org/openslx/taskmanager/tasks/BrandingGenerator.java b/src/main/java/org/openslx/taskmanager/tasks/BrandingGenerator.java new file mode 100644 index 0000000..add2351 --- /dev/null +++ b/src/main/java/org/openslx/taskmanager/tasks/BrandingGenerator.java @@ -0,0 +1,124 @@ +package org.openslx.taskmanager.tasks; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.geom.AffineTransform; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.io.FilenameUtils; +import org.openslx.satserver.util.Archive; +import org.openslx.satserver.util.Ppm; +import org.openslx.satserver.util.Util; +import org.openslx.taskmanager.api.AbstractTask; + +import com.google.gson.annotations.Expose; +import com.kitfox.svg.SVGDiagram; +import com.kitfox.svg.SVGUniverse; + +public class BrandingGenerator extends AbstractTask +{ + @Expose + private String tarFile = null; + + @Expose + private String svgFile = null; + + protected static final String[] ALLOWED_DIRS = + { "/tmp/", "/opt/openslx/configs/" }; + + private Output status = new Output(); + + @Override + protected boolean initTask() + { + this.setStatusObject( status ); + if ( this.svgFile == null || this.svgFile.isEmpty() ) { + status.error = "svgFile empty"; + return false; + } + if ( this.tarFile == null || this.tarFile.isEmpty() ) { + status.error = "tarFile empty"; + return false; + } + this.svgFile = FilenameUtils.normalize( this.svgFile ); + this.tarFile = FilenameUtils.normalize( this.tarFile ); + if ( !Util.startsWith( this.svgFile, ALLOWED_DIRS ) || !Util.startsWith( this.tarFile, ALLOWED_DIRS ) ) { + status.error = "svgFile or tarFile not in allowed dir"; + return false; + } + return true; + } + + @Override + protected boolean execute() + { + // Try to rasterize to png first - just to test if this is really an SVG image + // if this fails we still continue + File f = new File( this.svgFile ); + SVGUniverse svgUniverse = new SVGUniverse(); + File ppmFile = null; + try { + SVGDiagram diagram = svgUniverse.getDiagram( svgUniverse.loadSVG( f.toURI().toURL() ) ); + float scaling = 1; + if ( diagram.getHeight() > diagram.getWidth() ) + scaling = 192f / diagram.getHeight(); + else + scaling = 192f / diagram.getWidth(); + BufferedImage bi = new BufferedImage( (int) ( diagram.getWidth() * scaling ), (int) ( diagram.getHeight() * scaling ), BufferedImage.TYPE_INT_ARGB ); + Graphics2D rasterImage = bi.createGraphics(); + rasterImage.setClip( 0, 0, bi.getWidth(), bi.getHeight() ); + //rasterImage.setBackground( Color.WHITE ); + rasterImage.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); + AffineTransform at = new AffineTransform(); + at.setToScale( bi.getWidth() / diagram.getWidth(), bi.getWidth() / diagram.getWidth() ); + rasterImage.transform( at ); + diagram.render( rasterImage ); + rasterImage.dispose(); + try { + File tmpFile = File.createTempFile( "bwlp-", ".png", null ); + ImageIO.write( bi, "PNG", tmpFile ); + status.pngFile = tmpFile.getAbsolutePath(); + } catch ( Exception e ) { + status.error = "Can't create png preview: " + e.toString(); + } + try { + ppmFile = File.createTempFile( "bwlp-", ".ppm", null ); + Ppm.write( bi, ppmFile.getAbsolutePath(), Color.WHITE ); + } catch ( Exception e ) { + status.error = "Can't create png preview: " + e.toString(); + ppmFile = null; + } + } catch ( Throwable e ) { + status.error = "Can't rasterize image - maybe this is not a valid SVG file? - " + e.toString(); + } + TarArchiveOutputStream tar; + try { + tar = Archive.createTarArchive( this.tarFile ); + } catch ( IOException e ) { + status.error = "Can't write to " + this.tarFile + ": " + e.toString(); + return false; + } + Archive.tarAddFile( tar, "/etc/branding.svg", new File( this.svgFile ), 0644 ); + if ( ppmFile != null ) + Archive.tarAddFile( tar, "/etc/branding.ppm", ppmFile, 0644 ); + Util.multiClose( tar ); + return true; + } + + /** + * Output - contains additional status data of this task + */ + @SuppressWarnings( "unused" ) + private static class Output + { + protected String error = null; + protected String pngFile = null; + } + +} |