package projetMusee.util;

import java.awt.image.BufferedImage;

import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.ImageTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;

public class BufferedImageTranscoder extends ImageTranscoder {
    private BufferedImage image;

    @Override
    public BufferedImage createImage(int w, int h) {
        return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    }

    @Override
    public void writeImage(BufferedImage img, TranscoderOutput output) {
        this.image = img;
    }

    /**  
     * Transcode et renvoie l’image rasterisée.  
     * @param svgURI URI du SVG (file.toURI().toString())  
     * @param width largeur (0 = taille naturelle)  
     * @param height hauteur (0 = taille naturelle)  
     */
    public static BufferedImage transcode(String svgURI, float width, float height) throws Exception {
        BufferedImageTranscoder t = new BufferedImageTranscoder();
        if (width  > 0) t.addTranscodingHint(PNGTranscoder.KEY_WIDTH,  width);
        if (height > 0) t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, height);

        TranscoderInput input = new TranscoderInput(svgURI);
        t.transcode(input, null);
        return t.image;
    }
}
