Search in sources :

Example 1 with SvgImage

use of org.pentaho.di.core.svg.SvgImage in project pentaho-kettle by pentaho.

the class KettleXulLoader method getResourceAsStream.

@Override
public InputStream getResourceAsStream(String resource) {
    int height = iconHeight;
    int width = iconWidth;
    if (resource.contains(":")) {
        // we have height/width overrides
        width = Integer.parseInt(resource.substring(resource.indexOf(":") + 1, resource.indexOf("#")));
        height = Integer.parseInt(resource.substring(resource.indexOf("#") + 1, resource.indexOf(".")));
        resource = resource.substring(0, resource.indexOf(":")) + resource.substring(resource.indexOf("."));
    }
    if (SvgSupport.isSvgEnabled() && (SvgSupport.isSvgName(resource) || SvgSupport.isPngName(resource))) {
        InputStream in = null;
        try {
            in = super.getResourceAsStream(SvgSupport.toSvgName(resource));
            // load SVG
            SvgImage svg = SvgSupport.loadSvgImage(in);
            SwtUniversalImage image = new SwtUniversalImageSvg(svg);
            Display d = Display.getCurrent() != null ? Display.getCurrent() : Display.getDefault();
            // write to png
            Image result = image.getAsBitmapForSize(d, width, height);
            ImageLoader loader = new ImageLoader();
            loader.data = new ImageData[] { result.getImageData() };
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            loader.save(out, SWT.IMAGE_PNG);
            image.dispose();
            return new ByteArrayInputStream(out.toByteArray());
        } catch (Throwable ignored) {
        // any exception will result in falling back to PNG
        } finally {
            IOUtils.closeQuietly(in);
        }
        resource = SvgSupport.toPngName(resource);
    }
    return super.getResourceAsStream(resource);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SwtUniversalImageSvg(org.pentaho.di.core.SwtUniversalImageSvg) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SvgImage(org.pentaho.di.core.svg.SvgImage) Image(org.eclipse.swt.graphics.Image) SvgImage(org.pentaho.di.core.svg.SvgImage) SwtUniversalImage(org.pentaho.di.core.SwtUniversalImage) ImageLoader(org.eclipse.swt.graphics.ImageLoader) SwtUniversalImage(org.pentaho.di.core.SwtUniversalImage) Display(org.eclipse.swt.widgets.Display)

Example 2 with SvgImage

use of org.pentaho.di.core.svg.SvgImage in project pentaho-kettle by pentaho.

the class SwingDirectGC method getImageIcon.

private SwingUniversalImage getImageIcon(String fileName) throws KettleException {
    SwingUniversalImage image = null;
    InputStream inputStream = null;
    if (fileName == null) {
        throw new KettleException("Image icon file name can not be null");
    }
    if (SvgSupport.isSvgEnabled() && SvgSupport.isSvgName(fileName)) {
        try {
            inputStream = new FileInputStream(fileName);
        } catch (FileNotFoundException ex) {
        // no need to fail
        }
        if (inputStream == null) {
            try {
                inputStream = new FileInputStream("/" + fileName);
            } catch (FileNotFoundException ex) {
            // no need to fail
            }
        }
        if (inputStream == null) {
            inputStream = getClass().getResourceAsStream(fileName);
        }
        if (inputStream == null) {
            inputStream = getClass().getResourceAsStream("/" + fileName);
        }
        if (inputStream != null) {
            try {
                SvgImage svg = SvgSupport.loadSvgImage(inputStream);
                image = new SwingUniversalImageSvg(svg);
            } catch (Exception ex) {
                throw new KettleException("Unable to load image from classpath : '" + fileName + "'", ex);
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }
    if (image == null) {
        fileName = SvgSupport.toPngName(fileName);
        try {
            inputStream = new FileInputStream(fileName);
        } catch (FileNotFoundException ex) {
        // no need to fail
        }
        if (inputStream == null) {
            try {
                inputStream = new FileInputStream("/" + fileName);
            } catch (FileNotFoundException ex) {
            // no need to fail
            }
        }
        if (inputStream == null) {
            inputStream = getClass().getResourceAsStream(fileName);
        }
        if (inputStream == null) {
            inputStream = getClass().getResourceAsStream("/" + fileName);
        }
        if (inputStream != null) {
            try {
                BufferedImage bitmap = ImageIO.read(inputStream);
                WaitingImageObserver wia = new WaitingImageObserver(bitmap);
                wia.waitImageLoaded();
                image = new SwingUniversalImageBitmap(bitmap);
            } catch (Exception ex) {
                throw new KettleException("Unable to load image from classpath : '" + fileName + "'", ex);
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }
    if (image == null) {
        throw new KettleException("Unable to load image from classpath : '" + fileName + "'");
    }
    return image;
}
Also used : SwingUniversalImageSvg(org.pentaho.di.core.SwingUniversalImageSvg) KettleException(org.pentaho.di.core.exception.KettleException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SvgImage(org.pentaho.di.core.svg.SvgImage) SwingUniversalImageBitmap(org.pentaho.di.core.SwingUniversalImageBitmap) SwingUniversalImage(org.pentaho.di.core.SwingUniversalImage) FileInputStream(java.io.FileInputStream) KettleException(org.pentaho.di.core.exception.KettleException) FileNotFoundException(java.io.FileNotFoundException) BufferedImage(java.awt.image.BufferedImage) WaitingImageObserver(org.pentaho.reporting.libraries.base.util.WaitingImageObserver)

Example 3 with SvgImage

use of org.pentaho.di.core.svg.SvgImage in project pentaho-kettle by pentaho.

the class SwingGC method getImageIcon.

private SwingUniversalImage getImageIcon(String fileName) throws KettleException {
    SwingUniversalImage image = null;
    InputStream inputStream = null;
    if (fileName == null) {
        throw new KettleException("Image icon file name can not be null");
    }
    if (SvgSupport.isSvgEnabled() && SvgSupport.isSvgName(fileName)) {
        try {
            inputStream = new FileInputStream(fileName);
        } catch (FileNotFoundException ex) {
        // no need to fail
        }
        if (inputStream == null) {
            try {
                inputStream = new FileInputStream("/" + fileName);
            } catch (FileNotFoundException ex) {
            // no need to fail
            }
        }
        if (inputStream == null) {
            inputStream = getClass().getResourceAsStream(fileName);
        }
        if (inputStream == null) {
            inputStream = getClass().getResourceAsStream("/" + fileName);
        }
        if (inputStream != null) {
            try {
                SvgImage svg = SvgSupport.loadSvgImage(inputStream);
                image = new SwingUniversalImageSvg(svg);
            } catch (Exception ex) {
                throw new KettleException("Unable to load image from classpath : '" + fileName + "'", ex);
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }
    if (image == null) {
        fileName = SvgSupport.toPngName(fileName);
        try {
            inputStream = new FileInputStream(fileName);
        } catch (FileNotFoundException ex) {
        // no need to fail
        }
        if (inputStream == null) {
            try {
                inputStream = new FileInputStream("/" + fileName);
            } catch (FileNotFoundException ex) {
            // no need to fail
            }
        }
        if (inputStream == null) {
            inputStream = getClass().getResourceAsStream(fileName);
        }
        if (inputStream == null) {
            inputStream = getClass().getResourceAsStream("/" + fileName);
        }
        if (inputStream != null) {
            try {
                BufferedImage bitmap = ImageIO.read(inputStream);
                WaitingImageObserver wia = new WaitingImageObserver(bitmap);
                wia.waitImageLoaded();
                image = new SwingUniversalImageBitmap(bitmap);
            } catch (Exception ex) {
                throw new KettleException("Unable to load image from classpath : '" + fileName + "'", ex);
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }
    if (image == null) {
        throw new KettleException("Unable to load image from classpath : '" + fileName + "'");
    }
    return image;
}
Also used : SwingUniversalImageSvg(org.pentaho.di.core.SwingUniversalImageSvg) KettleException(org.pentaho.di.core.exception.KettleException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SvgImage(org.pentaho.di.core.svg.SvgImage) SwingUniversalImageBitmap(org.pentaho.di.core.SwingUniversalImageBitmap) SwingUniversalImage(org.pentaho.di.core.SwingUniversalImage) FileInputStream(java.io.FileInputStream) KettleException(org.pentaho.di.core.exception.KettleException) FileNotFoundException(java.io.FileNotFoundException) BufferedImage(java.awt.image.BufferedImage) WaitingImageObserver(org.pentaho.reporting.libraries.base.util.WaitingImageObserver)

Example 4 with SvgImage

use of org.pentaho.di.core.svg.SvgImage in project pentaho-kettle by pentaho.

the class SwingGUIResource method getUniversalImageIcon.

private SwingUniversalImage getUniversalImageIcon(PluginInterface plugin) throws KettleException {
    try {
        PluginRegistry registry = PluginRegistry.getInstance();
        String filename = plugin.getImageFile();
        ClassLoader classLoader = registry.getClassLoader(plugin);
        SwingUniversalImage image = null;
        if (SvgSupport.isSvgEnabled() && SvgSupport.isSvgName(filename)) {
            // Try to use the plugin class loader to get access to the icon
            // 
            InputStream inputStream = classLoader.getResourceAsStream(filename);
            if (inputStream == null) {
                inputStream = classLoader.getResourceAsStream("/" + filename);
            }
            // 
            if (inputStream == null) {
                inputStream = registry.getClass().getResourceAsStream(filename);
            }
            if (inputStream == null) {
                inputStream = registry.getClass().getResourceAsStream("/" + filename);
            }
            // 
            if (inputStream == null) {
                try {
                    inputStream = new FileInputStream(filename);
                } catch (FileNotFoundException e) {
                // Ignore, throws error below
                }
            }
            if (inputStream != null) {
                try {
                    SvgImage svg = SvgSupport.loadSvgImage(inputStream);
                    image = new SwingUniversalImageSvg(svg);
                } finally {
                    IOUtils.closeQuietly(inputStream);
                }
            }
        }
        if (image == null) {
            filename = SvgSupport.toPngName(filename);
            // Try to use the plugin class loader to get access to the icon
            // 
            InputStream inputStream = classLoader.getResourceAsStream(filename);
            if (inputStream == null) {
                inputStream = classLoader.getResourceAsStream("/" + filename);
            }
            // 
            if (inputStream == null) {
                inputStream = registry.getClass().getResourceAsStream(filename);
            }
            if (inputStream == null) {
                inputStream = registry.getClass().getResourceAsStream("/" + filename);
            }
            // 
            if (inputStream == null) {
                try {
                    inputStream = new FileInputStream(filename);
                } catch (FileNotFoundException e) {
                // Ignore, throws error below
                }
            }
            if (inputStream != null) {
                try {
                    BufferedImage bitmap = ImageIO.read(inputStream);
                    WaitingImageObserver wia = new WaitingImageObserver(bitmap);
                    wia.waitImageLoaded();
                    image = new SwingUniversalImageBitmap(bitmap);
                } finally {
                    IOUtils.closeQuietly(inputStream);
                }
            }
        }
        if (image == null) {
            throw new KettleException("Unable to find file: " + plugin.getImageFile() + " for plugin: " + plugin);
        }
        return image;
    } catch (Throwable e) {
        throw new KettleException("Unable to load image from file : '" + plugin.getImageFile() + "' for plugin: " + plugin, e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) SvgImage(org.pentaho.di.core.svg.SvgImage) SwingUniversalImage(org.pentaho.di.core.SwingUniversalImage) FileInputStream(java.io.FileInputStream) BufferedImage(java.awt.image.BufferedImage) WaitingImageObserver(org.pentaho.reporting.libraries.base.util.WaitingImageObserver) SwingUniversalImageSvg(org.pentaho.di.core.SwingUniversalImageSvg) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) SwingUniversalImageBitmap(org.pentaho.di.core.SwingUniversalImageBitmap)

Example 5 with SvgImage

use of org.pentaho.di.core.svg.SvgImage in project pentaho-kettle by pentaho.

the class KettleImageUtil method loadImages.

/**
 * Load multiple images from svg, or just png file.
 */
public static Image[] loadImages(XulDomContainer container, Shell shell, String resource) {
    Display d = shell.getDisplay();
    if (d == null) {
        d = Display.getCurrent() != null ? Display.getCurrent() : Display.getDefault();
    }
    if (SvgSupport.isSvgEnabled() && (SvgSupport.isSvgName(resource) || SvgSupport.isPngName(resource))) {
        InputStream in = null;
        try {
            in = getResourceInputStream(resource, container);
            // getResourceInputStream( SvgSupport.toSvgName( resource ) );
            // load SVG
            SvgImage svg = SvgSupport.loadSvgImage(in);
            SwtUniversalImage image = new SwtUniversalImageSvg(svg);
            Image[] result = new Image[IMAGE_SIZES.length];
            for (int i = 0; i < IMAGE_SIZES.length; i++) {
                result[i] = image.getAsBitmapForSize(d, IMAGE_SIZES[i], IMAGE_SIZES[i]);
            }
            return result;
        } catch (Throwable ignored) {
            // any exception will result in falling back to PNG
            ignored.printStackTrace();
        } finally {
            IOUtils.closeQuietly(in);
        }
        resource = SvgSupport.toPngName(resource);
    }
    InputStream in = null;
    try {
        in = getResourceInputStream(resource, container);
        return new Image[] { new Image(d, in) };
    } catch (Throwable ignored) {
    // any exception will result in falling back to PNG
    } finally {
        IOUtils.closeQuietly(in);
    }
    return null;
}
Also used : InputStream(java.io.InputStream) SwtUniversalImageSvg(org.pentaho.di.core.SwtUniversalImageSvg) SvgImage(org.pentaho.di.core.svg.SvgImage) SwtUniversalImage(org.pentaho.di.core.SwtUniversalImage) Image(org.eclipse.swt.graphics.Image) SvgImage(org.pentaho.di.core.svg.SvgImage) Display(org.eclipse.swt.widgets.Display) SwtUniversalImage(org.pentaho.di.core.SwtUniversalImage)

Aggregations

InputStream (java.io.InputStream)5 SvgImage (org.pentaho.di.core.svg.SvgImage)5 BufferedImage (java.awt.image.BufferedImage)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 SwingUniversalImage (org.pentaho.di.core.SwingUniversalImage)3 SwingUniversalImageBitmap (org.pentaho.di.core.SwingUniversalImageBitmap)3 SwingUniversalImageSvg (org.pentaho.di.core.SwingUniversalImageSvg)3 KettleException (org.pentaho.di.core.exception.KettleException)3 WaitingImageObserver (org.pentaho.reporting.libraries.base.util.WaitingImageObserver)3 Image (org.eclipse.swt.graphics.Image)2 Display (org.eclipse.swt.widgets.Display)2 SwtUniversalImage (org.pentaho.di.core.SwtUniversalImage)2 SwtUniversalImageSvg (org.pentaho.di.core.SwtUniversalImageSvg)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ImageLoader (org.eclipse.swt.graphics.ImageLoader)1 PluginRegistry (org.pentaho.di.core.plugins.PluginRegistry)1